2012-12-28 25 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