Zend2 组合表网关数组



我是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;
}

我的尺码表.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);

如何在控制器中将大小作为这些表的数组列出到价格中?

使用内部连接修复了它:

价格表.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);
            }
        }

最新更新