简体   繁体   中英

How to look for items in an arraycollection from a innerJoined entity in dql?

I want to select visits form users. The visits should contain tag names that are occuring in an array I am passing to dql.

Till now this is what I have:

    // select the user database
    $qb = $this->getEntityManager()->createQueryBuilder();
    $qb
        ->select('u')
        ->from('MyBundle:User', 'u');
        ->innerJoin('u.visits', 'v');
        ->innerJoin('v.tags', 't');

Now I want to look for visits that have tags ('t') with names ie 'VIP' AND 'Employee' AND ... etc. These names are dynamic, the array could be any length .

't' has a property name and is an arraycollection from 'v'
'v' is an arraycollection form 'u'

How do I do this?

Thank you.

您应该使用WHERE IN语句,将名称数组作为参数传递。

$qb->add('where', $qb->expr()->in('t.name', array('VIP', 'Employee', '...')));

i solved through recursion

    $qb = $this->getEntityManager()->createQueryBuilder();
    $qb
        ->select('u')
        ->from('MyBundle:User', 'u');
    $qb ->innerJoin('u.visits', 'v');
    $qb->andWhere($qb->expr()->in('v.id', $this->getVisitDQL($qb, $visitData, $tagCounter)));


public function getVisitDQL(QueryBuilder $qb, $data, $counter) {

    $tag = $data['tags'][$counter];
    $tagName = $tag['content'];

    $qb2 = $this->getEntityManager()->createQueryBuilder();

    $qb2
        ->select('v'.$counter.'.id')
        ->from('MyBundle:Visit', 'v'.$counter)
        ->innerJoin('v'.$counter.'.tags', 'vt'.$counter)
        ->where($qb2->expr()->eq('vt'.$counter.'.name',':vtag_'.$counter ));

    $qb->setParameter(':vtag_'.$counter, $tagName);

    // if were not processing the last filter, continue recursion
    if ($counter != (count($data['tags'])-1)) {
        $qb2->andWhere($qb->expr()->in('v'.$counter.'.id', $this->getVisitDQL($qb, $data, $counter + 1)));
    }

    return $qb2->getDQL();
}

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