2013-01-24 88 views
0

如何与另一个选择对象建立联接?我需要这样的查询Zend Framework 2 Zend Db 选择如何加入另一个选择

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

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

在ZF2连接方法的第一个参数是名称的表或具有别名的数组,但我不能在那里\选择。即使我放在这里

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

字符串被引用,并且查询无效。加入也不会添加括号。这真是令人困惑。

回答

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(); 
} 
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');