2012-12-28 34 views
0

我收到一個查詢錯誤,我的問題是:我可以鏈接連接嗎?zf1:鏈接加入

我的第一次連接是到主表,但我的第二次連接是連接到主表的表。這是查詢:

$query = $this->getDbTable()->select() 
      ->from(array('ca' => 'contracts_allotment'), 
        array('id', 
         'contracts_rooms_id' => new Zend_Db_Expr("CONCAT(room_type_desc, '-', room_characteristics_desc)") 
         )) 
      ->join(array('cr' => 'contracts_rooms'), 
        'ca.contract_rooms_id = cr.id', 
        array()) 
      ->join(array('rt' => 'room_types'), 
        'cr.room_id = rt.id', 
        array('room_type_desc')) 
      ->join(array('rc' => 'room_characteristics'), 
        'cr.char_id = rc.id', 
        array('room_characteristics_desc')) 
      ->where('contract_id = ?', $contractId); 

     var_dump($this->getDbTable()->fetchAll($query));die; 

我越來越:

Select查詢語句不能與其他表」

錯誤來參加由Zend/Db/Table/Select::assemble()

在這裏,你有一些內部裝配():

// Check each column to ensure it only references the primary table 
    if ($column) { 
     if (!isset($from[$table]) || $from[$table]['tableName'] != $primary) { 
      var_dump($from[$table]['tableName'], $primary);die; 
      require_once 'Zend/Db/Table/Select/Exception.php'; 
      throw new Zend_Db_Table_Select_Exception('Select query cannot join with another table'); 
     } 
    } 

var_dump()打印:

串(10) 「room_types」 字符串(19) 「contracts_allotment」

任何想法?

回答

1

做的時候不要忘了鎖表的連接:

$query = $this->getDbTable()->select() 
       ->setIntegrityCheck(false) 
       ->from(array('ca' => 'contracts_allotment'), 
        array('id', 
         'contracts_rooms_id' => new Zend_Db_Expr("CONCAT(room_type_desc, '-', room_characteristics_desc)") 
         )) 
       ->join(array('cr' => 'contracts_rooms'), 
        'ca.contract_rooms_id = cr.id', 
        array()) 
       ->join(array('rt' => 'room_types'), 
        'cr.room_id = rt.id', 
        array('room_type_desc')) 
       ->join(array('rc' => 'room_characteristics'), 
        'cr.char_id = rc.id', 
        array('room_characteristics_desc')) 
       ->where('contract_id = ?', $contractId); 

->setIntegrityCheck(false)至少應該給你一個新的錯誤。

+0

謝謝!作品! .... – ziiweb