[英]Zend Framework 2 how to get columns from multiple tables using join?
I am using ZfcBase\\Mapper\\AbstractDbMapper to write mysql queries for my project. 我正在使用ZfcBase \\ Mapper \\ AbstractDbMapper为我的项目编写mysql查询。 Everything works great for single table queries.
一切适用于单表查询。 If I join with some tables the results are good but the only problem is I dont know how to select columns from "joined" tables.
如果我与某些表联接,结果很好,但是唯一的问题是我不知道如何从“联接”表中选择列。 Here is my sample code:
这是我的示例代码:
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;
}
}
The result $myResults is actually an array of XYZ entities. 结果$ myResults实际上是XYZ实体的数组。 How can I have the ABC entities part of my result set $myResults?
如何在结果集中$ myResults中包含ABC实体?
Add ->setIntegrityCheck(false);
添加
->setIntegrityCheck(false);
I've updated your code: 我已经更新了您的代码:
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;
}
//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"';
Considering the above example found at examples . 考虑上述示例 。 Your code should look like this.
您的代码应如下所示。
$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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.