我想使用 Doctrine 结果映射,但我得到了一个非常奇怪的结果。
这是我在存储库“结果”中的代码:
$sql = 'SELECT * FROM (
SELECT
k.id AS keyword_id,
k.name,
r.position,
r.date,
r.id
FROM
Result r
INNER JOIN Keyword k ON r.keyword_id = k.id
WHERE r.domain_id = 2005
ORDER BY
date DESC
) AS foo
GROUP BY
CONCAT(name, DATE_FORMAT(date, \'%Y%m\') )
ORDER BY
CONCAT(name, date) ASC;';
$rsm = new ResultSetMapping;
$rsm->addEntityResult('XXX\SesBundle\Entity\Result', 'r');
$rsm->addFieldResult('r', 'id', 'id');
$rsm->addFieldResult('r', 'position', 'position');
$rsm->addFieldResult('r', 'date', 'date');
$rsm->addJoinedEntityResult('XXX\SesBundle\Entity\Keyword', 'k', 'r', 'keyword');
$rsm->addFieldResult('k', 'keyword_id', 'id');
$rsm->addFieldResult('k', 'name', 'name');
$em = $this->getEntityManager();
$results = $em->createNativeQuery($sql, $rsm)
->getResult();
return $results;
必须有 20 个结果,如果我count($results)
的结果是 20。
但是如果我 var_dump($result[19]) 我得到这个转储:
[19] =>
class Example\SesBundle\Entity\Result#1565 (7) {
private $id =>
int(498)
private $url =>
NULL
private $position =>
int(30)
private $date =>
class DateTime#1594 (3) {
public $date =>
string(19) "2012-10-02 12:04:17"
public $timezone_type =>
int(3)
public $timezone =>
string(13) "Europe/Berlin"
}
private $keyword =>
NULL
private $domain =>
NULL
private $engine =>
NULL
}
}
看,私人$keyword
是NULL
。正确的转储应该如下所示:
[16] =>
class Example\SesBundle\Entity\Result#1623 (7) {
private $id =>
int(19)
private $url =>
NULL
private $position =>
int(2)
private $date =>
class DateTime#1619 (3) {
public $date =>
string(19) "2012-09-28 17:33:26"
public $timezone_type =>
int(3)
public $timezone =>
string(13) "Europe/Berlin"
}
private $keyword =>
class Example\SesBundle\Entity\Keyword#1647 (3) {
private $id =>
int(2376)
private $name =>
string(19) "Example"
private $domain =>
class Doctrine\ORM\PersistentCollection#1624 (9) {
...
}
}
private $domain =>
NULL
private $engine =>
NULL
}
还有另一种非常奇怪的行为。结果被“转移”了。应该是的结果$result[0]
没有出现在var_dump()
. 如果执行的代码是$result[0]
应该是的结果$result[1]
,但是具有应该是的结果的 id $result[0]
。所以我认为映射不太正确。
如果我在 Navicat 中执行 SQL 语句,一切都是正确的。
对不起我的英语不好,我希望你明白我的意思:)