0

我收到一个查询错误,我的问题是:我可以链接连接吗?

我的第一个连接是主表,但我的第二个连接是连接到主表的表。这是查询:

$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;

我越来越:

选择查询不能与另一个表连接”

错误来自Zend/Db/Table/Select::assemble()

这里有一些内部 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)“房间类型”字符串(19)“合同分配”

任何的想法?

4

1 回答 1

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)至少应该给你一个新的错误。

于 2012-12-28T13:44:15.290 回答