【问题标题】:Symfony doctrine findby => columnValue is contained inSymfony 学说 findby => columnValue 包含在
【发布时间】:2016-10-16 20:30:00
【问题描述】:

我有一个用户实体,该实体具有关联的扇区(例如扇区 A、B、C 等)

(部门也是一个实体)

然后我有一个文档实体,它也有关联的扇区

我想检索其扇区在用户实体中的所有文档...

教义如何做到这一点?

$repo    = $this->getDoctrine()->getRepository('MyBundle:Document');
$sectors = $this->getUser()->getSectors();

$repo->findBy(['sectors'=>???]);

【问题讨论】:

    标签: php doctrine-orm doctrine symfony


    【解决方案1】:

    这个应该够了:

    $repo = $this->getDoctrine()->getRepository('MyBundle:Document');
    $sectors = $this->getUser()->getSectors();
    $sectorIds = array_map(function($sector) { return $sector->getId(); }, $sectors);
    
    $queryBuilder = $repo->createQueryBuilder('document');
    $queryBuilder
        ->lefJoin('document.sectors', 'sector')
        ->where('sector.id IN (:sectorIds)')
        ->setParameter('sectorIds', $sectorIds);
    
    $documents = $queryBuilder->getQuery()->getResult();
    

    灵感来自How to use WHERE IN with Doctrine 2

    【讨论】:

    • 不,它不会起作用,因为文档可以有多个扇区。例如,用户有 A、B 扇区,Doc1 有 A、B、C 扇区,而 Doc2 有 A 扇区。所以我只需要检索(在这种情况下)Doc2(因为 Doc1 也是用户没有的扇区 C)
    猜你喜欢
    • 1970-01-01
    • 2014-05-08
    • 1970-01-01
    • 2012-06-11
    • 2012-03-04
    • 1970-01-01
    • 2018-06-27
    • 1970-01-01
    • 2016-03-31
    相关资源
    最近更新 更多