zf1:链式连接

时间:2012-12-28 13:33:52

标签: zend-framework

我收到查询错误,我的问题是:我可以连接加入吗?

我的第一次加入是主表,但我的第二次加入是加入主表的表。这是查询:

$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()

这里有一些内部汇编():

   // 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()打印:

  

string(10)“room_types”string(19)“contracts_allotment”

有什么想法吗?

1 个答案:

答案 0 :(得分: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)至少应该给你一个新错误。