mysql - Doctrine2 query(Builder) loses relations after using "where" and "in" clause -


i have 2 entities, let's call them "girl" , "feature". entity "girl" in undirectional manytomany relation "feature".

i have page simple search form. want search girls features. after form submission on server side list of features id's, , should return list of girls have selected features listed with other features, girl have.

let's @ this: ╔════════════╦═══════════════╦═══════════════╦═══════════════╗ ║ ║ (1) feature 1 ║ (2) feature 2 ║ (3) feature 3 ║ ╠════════════╬═══════════════╬═══════════════╬═══════════════╣ ║ (1) girl 1 ║ yes ║ yes ║ no ║ ║ (2) girl 2 ║ yes ║ no ║ yes ║ ╚════════════╩═══════════════╩═══════════════╩═══════════════╝

numbers in brackets entities id.

what @ first is:

$qbd = $this->getqbd('appbundle:girl', 'g')         ->select('g, gf')         ->leftjoin('g.features', 'gf'); 

after getting data (features ids) url (becausce form use method) , putting them $features array, perform this:

if (!empty($features)) {     $qbd->where(         $qbd->expr()->in('gf.id', ':features')     )->setparameter('features', $features); } 

simple possible.

everything fine. almost.

when try perform search girls features id equals 2 , 3, in result both girls (which good). sadly, each 1 of them have feature 2 , 3 (for case; when i'm trying perform each of them $girl->getfeatures()), if both of them should contains reference feature 1.

what i'm doing wrong? i've tried build others, more complex queries case, each of them @ end got same problem.

you can make use of "member of" dql construction.

for example can so:

$qb->from('appbundle:girl', 'g')    ->select('g')    ->andwhere(':feature member of g.features')    ->setparameter('feature', $feauture); 

the bad thing work single feature. example, if u need girls having feature , b u need so:

$qb->from('appbundle:girl', 'g')    ->select('g')    ->andwhere(':featurea member of g.features')    ->setparameter('featurea', $featurea)    ->andwhere(':featureb member of g.features')    ->setparameter('featureb', $featureb); 

it cleanest way search entity in collection know. but, if need search entity (girl) have many related entities (features) gonna face performance issues using approach. in case maybe better make use of sub-queries.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -