1

如何与另一个选择对象进行连接?我需要这样的查询

$select->join(array('alias' => $subSelect), 'on clause', 'array('*'), 'left');

这样的事情在 ZF1.x 中是可能的。

在 ZF2 中,join 方法的第一个参数是带有别名的表或数组的名称,但我不能放在那里 \Select。即使我放在这里

$select->getSqlString($this->adapter->platform);

字符串被引用,并且查询无效。Join 也不会添加括号。这真的很混乱。

4

2 回答 2

1

也许这会对你有所帮助。这是我在 TableGateway 中的一个工作示例:

public function Profile($params)
{
    $result = $this->select(function (Select $select) use ($params) {
        $select
            ->columns(array(
                'ipaddress_type',
                'ipaddress',
                'domain'
            ))
            ->join('product_hosting_profile', 'product_hosting_profile.productid = webaccount.productid', array(
                'servers',
                'services'
            ))
            ->where(array(
                $this->adapter->getPlatform()->quoteIdentifierChain(array('webaccount', 'accountid')) . ' = ' . $this->adapter->getPlatform()->quoteValue($params['accountid']),
                $this->adapter->getPlatform()->quoteIdentifierChain(array('webaccount', 'productid')) . ' = ' . $this->adapter->getPlatform()->quoteValue($params['productid']),
                $this->adapter->getPlatform()->quoteIdentifierChain(array('webaccount', 'webaccountid')). ' = ' . $this->adapter->getPlatform()->quoteValue($params['webaccountid'])
            ))
            ->limit(1);
    });

    return $result->current();
}
于 2013-01-25T00:21:29.353 回答
0
$subSelect=new Select(array('A'=>new TableIdentifier('target','CAT')));
    $subSelect->columns(array('fk_w','TI'=>'id','mtarget_f'=>new Expression('sum(F)'),
                'mcost'=>new Expression('round(sum(F*D.mcost),2)')))
    ->join(array('B'=>new TableIdentifier('item','CAT')),'B.id=A.fk_w',
            array())
                    ->join(array('D'=>new TableIdentifier('rate','CAT')),'A.dat=D.year and D.fk_c=B.CCODE and D.fk_b=A.fk_b',
                            array())
                            ->where->equalTo('Year', $year);



    $select=new Select(array('A'=>new TableIdentifier('item','CAT')));
    $select->columns(array('WID'=>'id','AID'=>'AID',
                'item'=>'ITEM_ID','Wtitle'=>'WTITLE'))
    ->join(array('B'=>new TableIdentifier('cn','CAT')),'B.id=A.CCODE',
            array('Ct'=>'name'))
    ->join(array('S'=>$subSelect),'S.fk_w=A.id',array('Mtarget'=>'mcost',
                    'MTarget'=>'m_target',                  
                    'TID'),'left');
于 2016-10-11T08:28:15.663 回答