简体   繁体   中英

symfony doctrine: find objects where one of the one-to-many-associations is like value

I have an Entity named "customer". At this entity there is an OneToMany-association to an other entity "customer-status". I want to get all customer-objects where ONE of the customer-status-fields is like xyz.

This is not a problem if i make a QB like this:

$qb->andWhere($qb->expr()->like('s.comment', ':comment'));
$qb->setParameter('comment', "Created at %");

The only problem about this is, that i get all customers with the customer-statuses like the query. But only these statuses. I want ALL statuses if the like-query applies.

I have searched for this but found nothing like this. Anyone an idea?

After a few beers i found the anser. For the expected result i should not define the customer-status entity in the select-operation. Just select the parent-entity. So this:

$qb
    ->select(['c'])
    ->leftJoin('customer.statuses', 's')
    ->andWhere($qb->expr()->like('s.comment', ':comment'))
    ->setParameter('comment', "Created at %");

is the right way instead of

$qb
    ->select(['c', 's'])
    ->leftJoin('customer.statuses', 's')
    ->andWhere($qb->expr()->like('s.comment', ':comment'))
    ->setParameter('comment', "Created at %");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM