【发布时间】:2015-03-28 00:25:50
【问题描述】:
我在自定义存储库 symfony 2 类中有这段代码,我很难理解为什么 $results 总是为空。
MyApplicationBundle:Archive 是映射到 DB 的实体类
集合是连接表 (ManyToMany) 不是实体
当前不是此类的属性,但我在结果中需要它
$where_str 是在上面构建的
我做错了什么?谢谢
$rsm = new ResultSetMapping();
$rsm->addEntityResult('MyApplicationBundle:Archive', 'a');
$rsm->addFieldResult('a', 'id', 'id');
$rsm->addFieldResult('a', 'author', 'author');
$rsm->addFieldResult('a', 'title', 'title');
$rsm->addFieldResult('a', 'present', 'present');
$sql = "select
a.id,
a.author,
a.title,
IF((select c.archive_id from collection c where c.archive_id = a.id and c.user_id = :user),1,0) as present
from
archive a
where
$where_str";
$em = $this->getEntityManager();
$query = $em->createNativeQuery($sql, $rsm);
$query->setParameters($arrParameters);
$results = $query->getResult();
return $results;
如果更好的话,非常乐意尝试使用 DQL 或 DQB。
【问题讨论】:
标签: mysql sql symfony doctrine