原则 - 在一个查询中获取实体和关系 [英] Doctrine - Get entity and relationships in one query
问题描述
当然,我创建的映射/注释,Doctrine应该能够进行几个连接和一个查询,而无需编写一个DQL查询来检索每个实体。
或者,正如我预测的那样,我错过了某个地方!
只需将相关实体的别名添加到选择
部分查询。
说,你有预订
相关的一对多到封面
,你想要这样选择一些书籍的封面
使用查询构建器,使用:
- > createQueryBuilder ()
- > select(book,cover)
- > from(Book,book)
- > leftJoin(book.cove rs,cover)
使用查询,使用:
SELECT book,cover FROM Book book LEFT JOIN book.covers cover
因此,您将收到 Book
的集合,其中填充了 $ cover
collection。 / p>
Am I missing a point with Doctrine? It's been very useful for some scenarios, but for the basic scenario of retrieving an entity using an Id (say using find() or findOneBy()) why do you see a query being fired off for every relationship to populate the main entity's properties?
Surely with the mapping/annotations I've created, Doctrine should be capable of a few joins and one query, without having to write a DQL query for the retrieval of each entity.
Or, as I'm predicting, have I missed the point somewhere!
Just add the aliases of related entities to select
part of your query.
Let’s say, you have Book
related one-to-many to Cover
, and you want so select some books with their covers.
With query builder, use:
->createQueryBuilder()
->select("book, cover")
->from("Book", "book")
->leftJoin("book.covers", "cover")
With query, use:
SELECT book, cover FROM Book book LEFT JOIN book.covers cover
As the result, you will receive collections of Book
with prepopulated $covers
collection.
这篇关于原则 - 在一个查询中获取实体和关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!