Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering on association with criteria object in Doctrine 2

Say that I have an entity Profile which has an association with an Account entity. I want to fetch the profile with profileCode = 12345 and where its related Account has the e-mail address of [email protected]. So, I need to specify a condition on both entities.

For this I have created a custom repository Repository\Profile and now I am wondering how to implement this. I know that I can solve all of this with a "raw" DQL query or by using the query builder. However, I feel as if it is not as pretty as I would like, because it is very close to raw SQL. Sure the syntax is a bit different, but conceptually, I'd be thinking more in terms of SQL than OOP. I will be doing these kind of things a lot, so I am really trying to do it the best way.

I have done a bit of reading on the Criteria object (the documentation is sparse), and it really seems great as long as I am filtering on a single entity. I was unable to find any solution to using Criteria when filtering on associated entities.

So basically my question is: is there any way in which I can use the Criteria object for filtering on multiple entities directly from the database, and within a custom repository? I would really prefer to code this within a custom repository than the entity itself as I have seen some people do. Is this possible, or are there any good alternatives, or is plain DQL or the query builder really the way to go?

like image 235
ba0708 Avatar asked Mar 26 '14 19:03

ba0708


People also ask

What's in the doctrine 2 Orm documentation?

Welcome to Doctrine 2 ORM's documentation! The Doctrine documentation is comprised of tutorials, a reference section and cookbook articles that explain different parts of the Object Relational mapper. Doctrine DBAL and Doctrine Common both have their own documentation. Getting Help

How does doctrine detect when a new entity is created?

During each flush()operation Doctrine detects if there are new entities in any collection and three possible cases can happen: New entities in a collection marked as cascade: persistwill be directly persisted by Doctrine. New entities in a collection not marked as cascade: persistwill produce an Exception and rollback the flush()operation.

What is the doctrine documentation like?

The Doctrine documentation is comprised of tutorials, a reference section and cookbook articles that explain different parts of the Object Relational mapper. Doctrine DBAL and Doctrine Common both have their own documentation. Getting Help If this documentation is not helping to answer questions you have about Doctrine ORM don't panic.

How does doctrine apply to a cascade operation?

For each cascade operation that gets activated, Doctrine also applies that operation to the association, be it single or collection valued. Persistence by Reachability: Cascade Persist There are additional semantics that apply to the Cascade Persist operation.


1 Answers

Criteria can only filter the associations on the entity itself, but if I understand your use case, you need to filter 2 levels deep, so it won't work.

You are trying to shape Doctrine into something that is not, by wanting to do things "your way". It will only hurt you in the long run. The solution is already there, it is perfectly fine to use DQL, especially in repositories.

like image 183
K. Norbert Avatar answered Oct 02 '22 05:10

K. Norbert