内心的联系多对多

时间:2018-09-06 17:29:49

标签: inner-join symfony4 doctrine-query

我的symfony项目中有两个实体:房屋和软件。

许多家庭可以具有许多软件,许多软件可以属于许多家庭

我正在尝试仅获取具有以下特征的房屋:假设软件n°1 +软件n°2。

实际上,我已经设法检索了拥有软件n°1的房屋和拥有软件n°2的房屋,但并非同时拥有软件1 + soft2的房屋

如果我没记错,应该是内部联接联接,对吧?

这是我的实体和存储库的方法:

    class Software {
          /**
           * @ORM\ManyToMany(targetEntity="App\Entity\Home", mappedBy="softwares")
           */
           private $homes;

           public function __constuct() {
               $this->homes = new ArrayCollection();
           }

           // ...

           public function getHomes(){ ... }
           public function addHome(Home $home){ ... }
           // ...

    }


    class Home {
          /**
           * @ORM\ManyToMany(targetEntity="App\Entity\Software", inversedBy="homes")
           */
           private $softwares;

           public function __constuct() {
               $this->softwares = new ArrayCollection();
           }

           //...

           public function getSoftwares(){ ... }
           public function addSoftware(Software $software){ ... }
           //...

    }

主存储库

    class HomeRepository extends ServiceEntityRepository {
          public function innerJoinSoftware($softIds)
          {
               $qb = $this->createQueryBuilder('c')
                   ->innerJoin('c.softwares', 's')
                   ->andWhere('s.id IN(:softIds)')
                     ->setParameter('softIds', $softIds)
              ;
              return $qb->getQuery()->getResult();
         }
    }

为了说明我的观点:

  • Home1具有soft1,soft2

  • Home2具有soft1,soft3

  • Home3具有soft2,soft3

我想做的是

  dump(homeRepo->innerJoinSoftware([1, 2]));
  //should output Home1 but actually I have
  //it outputs Home1, Home2, Home3

这是我发布的SQL版本,但是我仍然无法使用Doctrine完成

  SELECT home.id, home.name FROM Home as home
  INNER JOIN (
        SELECT home_id as home_id, COUNT(home_id) as count_home
        FROM home_software
        WHERE software_id IN (1, 2)
        GROUP BY home_id
        HAVING count_home = 2) as soft # count_home should be dynamic 
  ON home.id = soft.home_id
  ORDER BY home.name

1 个答案:

答案 0 :(得分:0)

这是我解决此问题的方法(在我发布的原始SQL的帮助下)

public function findBySoftwaresIn($softIds)
{
    //retrieve nbr of soft per home
    $nbrSoftToFind = count($softIds);
    $qb = $this->createQueryBuilder('h');

    $qb->innerJoin('h.softwares', 's')
        ->andWhere('h.id IN (:softIds)')
            ->setParameter('softIds', $softIds)

        //looking for home coming back by nbrSoft
        ->andHaving($qb->expr()->eq($qb->expr()->count('h.id'), $nbrSoftToFind))
        ->groupBy('h.id')//dont forget to group by ID
        ->addOrderBy('h.name')
    ;

    return $qb->getQuery()->getResult();
}