New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DDC-1007: Add ability to filter a association #1592
Comments
Comment created by @beberlei:
By which standard are you making this assumption? |
Issue was closed with resolution "Duplicate" |
Comment created by enumag: Just some examples: http://www.doctrine-project.org/docs/orm/2.0/en/reference/limitations-and-known-issues.html#restricing-associations You need to use DQL even for the most easiest tasks. There is no way to implement methods like "getVisibleArticles", everything must be done through repositories and DQL. Therefor the associations are almost useless as they are now. There is almost always need for some filter and this logic should be in entities themselves. I mean I of course can make new repository class for every entity, but then why are we using ORM which makes every task even more difficult then it is without the ORM? I've been looking for some way to manually modify the SQL query used to load the collection, but found none. //EDIT: |
Comment created by mjh_ca: No need for this. This is exactly what custom repositories are for. [http://www.doctrine-project.org/docs/orm/2.0/en/reference/working-with-objects.html#custom-repositories] If you don't like writing raw DQL, use the QueryBuilder class instead to build up your query.
|
Comment created by @beberlei: I do think its very necessary to have this, but we are still evaluating how this can be done generically. |
Comment created by parljohn: I tend to agree. I have just spent an unfortunate amount of time trying to figure out how to filter an association without loading the whole thing. I figured i could easily use a custom repository and was headed down that path until it dawned on me that I'm breaking the whole point of doctrine to use an Entity Manager inside of an existing entity... which is the only way I can call a custom repository from inside of an entity. In order to keep my code clean I need a way to get an association and filter it without loading the whole thing. Tell me how else I would do that without breaking clean coding rules? Even if you could just use custom Collection loaders from a custom repository that would be great but it doesn't appear that when you load a collection you're not even calling a Repository to do it, if you were, I would have found a way to override that function and add some filtering devices to it. This seems to make complete sense to me? What am I missing here about Jachyms request? |
Comment created by @beberlei: There is currently no way to "just override" some function, as I said its really complicated to implement and we are evaluating the best way to do it. If you want to filter associations use DQL for now. |
Comment created by parljohn: Awesome, thanks for the quick response. Just leaves me with 2 questions then that would help me a ton:
To clarify, I have an entity with an association that is NULL for a specific filter and I need it to stay null, but I can't because it autoloads all the other records that don't match that filter when I ask for it. I have no way to "redo" the filter I originally used in the DQL. Thanks again for the quick response. |
Comment created by @beberlei:
|
Comment created by parljohn: So based on my clarification above, how do you suggest I call a DQL from inside of an entity? Call it inline in one my methods through an entity manager? or through an entity manager to a custom repository? I don't see any other ways, am I missing anything? My problem is the reverse of what you are solving with Extra Lazy Loading, I need a collection to stay empty based on a my DQL, but it won't! It keeps calling all associations. |
Comment created by enumag: Hi, is there a way to implement this in current Doctrine? I'm talking about this method (it should be a method of CategoryEntity): public function getArticles($visibility = 0, $limit = NULL) { return /** what should be here? **/ } |
Comment created by iamkoby: A year later this is still highly required feature. |
Comment created by alexandresalome: Found this in documentation, related to this ticket: |
Imported 1 attachments from Jira into https://gist.github.com/b853bc3327fe797e02a1 |
Jira issue originally created by user enumag:
Note: I used critical priority for this issue because this is one of the most criticised things about Doctrine 2 worldwide.
The worst thing about Doctrine 2 is, that you can do almost nothing through methods like Category::getArticles();.
For example, you have the Category and want to output all the articles. Fine, nothing easier:
foreach ($category->getArticles() as $article) {
echo $article->name;
}
OK. And now I need only "visible" articles (others are hidden for now). And here it is. You need DQL even for this easy task. Without DQL you can't do nothing, but using DQL (or QueryBuilder) everywhere is very very bad. Simply there is no way to write method like getVisibleArticles(). (Except selecting all and using some filter, but that's nonsense.)
The solution would be some kind of smart collections (but i don't mind if you come up with something different):
public function getArticles($visibility = 0, $limit = NULL) {
return $this->articles->filter('visibility', $visibility)->limit($limit);
}
Of course when you need any AND, OR, XOR, IN, LIKE and so DQL is needed. This should be for some simple things only.
The text was updated successfully, but these errors were encountered: