Doctrine2 subquery

旧街凉风 提交于 2019-12-13 02:28:10

问题


I'm trying to write a subquery in doctrine2, to sort a table ordered by date column of another table.

(let's say I'm querying table A, which has an id column, and B has an a_id and a date, in the subquery b.a_id = a.id)

I'm using query builder, and the addSelect method, but since I can't use LIMIT in my query I get this error:

SQLSTATE[21000]: Cardinality violation: 1242 Subquery returns more than 1 row

This error is true, but how could I limit this query to 1 row, if LIMIT is not allowed in doctrine2 and when I try to do it by querybuilder (I mean the subquery) and I'm using setMaxResults, and then getDQl it is still not working.

->addSelect('(SELECT b.date FROM B b WHERE b.conversation = a.id ORDER BY b.date DESC)')

Is there any solution for my problem?

Thanks


回答1:


Make the query return exactly one row. Try SELECT MAX(b.date) FROM B b WHERE b.conversation = a.id)



来源:https://stackoverflow.com/questions/7879683/doctrine2-subquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!