Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 findBy method with property from relationship

I'm wondering if it's possible to querying doctrine entities by a property from a relation.

Here is an example :

Entity A fields :


-> title
-> content
-> description
-> date


Entity B fields :

-> title
-> link ( entity b )
-> date

Is it possible to querying Entity B by link->title property such as the following :

$this->getDoctrine()->getManager()->getRepository("acmeAppBundle:EntityB")->findBy(array( "title" => "test", "link.title" => "example" ) );

Currently I'm achieving this with a custom function from Entity B repository, but may be I'm missing something.

like image 206
Charles-Antoine Fournel Avatar asked Oct 18 '25 00:10

Charles-Antoine Fournel


1 Answers

You can't use findBy like this. FindBy is only there to fetch very basic things. Generally it's considered as a best practice to use repository calls, because for example, if you here fetch all objects where title is test and then you get from A all B entity, then entity B will be fetched separately, while in a repository call you can use join, so only one query will be sent to your DB.

like image 108
ghostika Avatar answered Oct 19 '25 18:10

ghostika



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!