Zend Framework 2如何使用join从多个表中获取列?

时间:2014-04-14 05:56:04

标签: php mysql sql zend-framework

我正在使用ZfcBase \ Mapper \ AbstractDbMapper为我的项目编写mysql查询。一切都适用于单表查询。如果我加入一些表格,结果很好,但唯一的问题是我不知道如何从“已加入”表中选择列。这是我的示例代码:

class XYZ extends AbstractDbMapper implements XYZInterface

    {
    public function joinTables() {
         $select = $this->getSelect();
         $select->reset('columns');
         $select->columns(array('colA','colB'))
                   ->join('ABC','ABC.colA=XYZ.colA',array('colB','colC'))
                   ->where(array('XYZ.colA' => 'value1'));
         $resultSet = $this->select($select);
         $myResults= array();
         foreach ($resultSet as $myResult) {
                $myResults[] = $myResult;
         }
         return $myResults;
    }

}

结果$ myResults实际上是一个XYZ实体数组。如何让ABC实体成为我的结果集$ myResults的一部分?

2 个答案:

答案 0 :(得分:0)

添加->setIntegrityCheck(false);

我已更新您的代码:

public function joinTables() {
     $select = $this->getSelect();
     $select->setIntegrityCheck(false);
     $select->reset('columns');
     $select->columns(array('colA','colB'))
               ->join('ABC','ABC.colA=XYZ.colA',array('colB','colC'))
               ->where(array('XYZ.colA' => 'value1'));
     $resultSet = $this->select($select);
     $myResults= array();
     foreach ($resultSet as $myResult) {
            $myResults[] = $myResult;
     }
     return $myResults;
}

答案 1 :(得分:0)

//join with columns

    $select11 = new Select;
    $select11->from('foo')->join('zac', 'm = n', array('bar', 'baz'));
   //'SELECT "foo".*, "zac"."bar" AS "bar", "zac"."baz" AS "baz" FROM "foo" INNER JOIN "zac" ON "m" = "n"';

考虑在examples找到的上述示例。您的代码应如下所示。

 $select = $this->getSelect();
 $select->from('ABC')->join('XYZ','ABC.colA=XYZ.colA' array('colA','colB'))
                   ->where('XYZ.colA = value1');
         $resultSet = $this->select($select);
         $myResults= array();