Zend2结合了tablegateway阵列

时间:2014-04-06 12:13:15

标签: sql arrays zend-framework2

我是Zend2的新手,想要组合两个tablegateway对象

我有两张桌子:价格和尺寸。每个价格都有多种尺寸。 我想将这些表连接到一个数组中,因此我可以列出其中包含大小的价格。

例如:

array(
        1 => array(
                'price' => 45,
                'description' => 'Lorem ipsum',
                'sizes' => array(
                    1 => '16',
                    2 => '17',
                    3 => '20',
                    4 => '21'
                )
        ),
        2 => array(
                'price' => 50,
                'description' => 'Lorem ipsum',
                'sizes' => array(
                    1 => '34',
                    2 => '12',
                    3 => '21',
                    4 => '50'
                )
        )
    )

我的价格表.php:

public function getPricesbyJewel($jewel)
{
    $rowset = $this->tableGateway->select(array('jewelid' => $jewel));
    return $rowset;
}

My SizesTable.php

public function getSizesbyPrice($price)
{
    $rowset = $this->tableGateway->select(array('priceid' => $price));
    return $rowset;
}

我如何列出价格(因此没有尺寸)

$jewelPrices = array('jewelryPrices' => $this->getPricesTable()->getPricesbyJewel($jewel->id));
$jewelSizes = array('jewelrySizes' => $this->getSizesTable()->getSizesbyPrice($priceID);

如何将尺寸列为价格,作为控制器中这些表的数组?

1 个答案:

答案 0 :(得分:0)

使用内部联接修复它:

PriceTable.php

public function getPricesbyJewel($jewel)
{
    $sql = new Sql($this->tableGateway->getAdapter());

    $select = $sql->select();
    $select->from('jewelry_prices')
        ->join('jewelry_sizes', 'jewelry_prices.id=jewelry_sizes.priceID')
        ->where('jewelry_prices.jewelid='.$jewel);
    $resultSet = $this->tableGateway->selectWith($select);
    return $resultSet;
}

控制器:

$prices = array('jewelryPrices' => array());
foreach($this->getPricesTable()->getPricesbyJewel($jewel->id) as $key => $price){
            if(!array_key_exists($price->priceID, $prices['jewelryPrices'])){
                $prices['jewelryPrices'][$price->priceID] = array(
                    'orderNote' => $price->orderNote,
                    'price'     => $price->price,
                    'description' => $price->description,
                    'sizes'     => array()
                );
                array_push($prices['jewelryPrices'][$price->priceID]['sizes'], $price->size);
            } else {
                array_push($prices['jewelryPrices'][$price->priceID]['sizes'], $price->size);
            }
        }