如何访问 ManyToMany 关系上的 LEFT JOIN 属性?
How to access an attribute of a LEFT JOIN on a ManyToMany relation?
我有三个实体与各种关系相关联:
-Band-
name
...
tours (ManyToMany with Tour)
shows (OneToMany with Show)
-Tour-
name
...
bands (ManyToMany with Band)
$shows(OneToMany with Show)
-Show-
date
...
band(ManyToOne with Band, nullable)
tour(ManyToOne with Tour, nullable)
我可以为 Band 设置 Show(然后 show_tour 为 NULL),也可以为 Tour 设置 Show(然后 show_band 为 NULL)。
现在,我想获得给定 Band
的所有 Show
。我的 DQL 是这样的:
public function findAllShowsToComeFor($band)
{
$date = new \DateTime('now');
return $this->createQueryBuilder('s')
->leftJoin('s.band', 'band')
->where('band.id = :bid')
->setParameter('bid', $band->getId())
->leftJoin('s.tour', 'tour')
->where('tour.bands = :tid')
->setParameter('tid', $band->getId())
->andWhere('s.day >= :date')
->setParameter('date', $date->format('Y-m-d'))
->orderBy('s.day', 'ASC')
->getQuery()
->getResult();
}
当然,这会引发语法错误([语义错误] 第 0 行,'bands = :tid' 附近的第 92 列:错误:无效的 PathExpression。预期 StateFieldPathExpression 或 SingleValuedAssociationField。),因为这些行:
->leftJoin('s.tour', 'tour')
->where('tour.bands = :tid')
->setParameter('tid', $band->getId())
我需要做类似的事情:
->leftJoin('s.tour', 'tour')
->where('tour.bands.id IN :tid')
->setParameter('tid', $band->getId())
但这不可能...
有人能帮忙吗?
如果您已经在 Band 上进行过 Tour,则无需在 Band ID 上过滤 Tour。
所以这会给你这个:
$this->createQueryBuilder('s')
->join('s.band', 'band')
->where('band.id = :bid')
->leftJoin('s.tour', 'tour')
->where('s.day >= :date')
->orderBy('s.day', 'ASC')
->setParameter('date', $date->format('Y-m-d'))
->setParameter('bid', $band->getId())
->getQuery()
->getResult();
有了这个你会得到一个 Show 的 ArrayCollection,如果你在它上面做一个 ->getTour 你可能会有一个 Tour(或 NULL)。
有MEMBER OF
,相信你想要的是这样的:
$this->createQueryBuilder('s')
->leftJoin('s.tour', 'tour')
->where('s.band = :band OR :band MEMBER OF tour.bands')
->setParameter('band', $band)
->andWhere('s.day >= :date')
->setParameter('date', $date->format('Y-m-d'))
->orderBy('s.day', 'ASC')
->getQuery()
->getResult();
我有三个实体与各种关系相关联:
-Band-
name
...
tours (ManyToMany with Tour)
shows (OneToMany with Show)
-Tour-
name
...
bands (ManyToMany with Band)
$shows(OneToMany with Show)
-Show-
date
...
band(ManyToOne with Band, nullable)
tour(ManyToOne with Tour, nullable)
我可以为 Band 设置 Show(然后 show_tour 为 NULL),也可以为 Tour 设置 Show(然后 show_band 为 NULL)。
现在,我想获得给定 Band
的所有 Show
。我的 DQL 是这样的:
public function findAllShowsToComeFor($band)
{
$date = new \DateTime('now');
return $this->createQueryBuilder('s')
->leftJoin('s.band', 'band')
->where('band.id = :bid')
->setParameter('bid', $band->getId())
->leftJoin('s.tour', 'tour')
->where('tour.bands = :tid')
->setParameter('tid', $band->getId())
->andWhere('s.day >= :date')
->setParameter('date', $date->format('Y-m-d'))
->orderBy('s.day', 'ASC')
->getQuery()
->getResult();
}
当然,这会引发语法错误([语义错误] 第 0 行,'bands = :tid' 附近的第 92 列:错误:无效的 PathExpression。预期 StateFieldPathExpression 或 SingleValuedAssociationField。),因为这些行:
->leftJoin('s.tour', 'tour')
->where('tour.bands = :tid')
->setParameter('tid', $band->getId())
我需要做类似的事情:
->leftJoin('s.tour', 'tour')
->where('tour.bands.id IN :tid')
->setParameter('tid', $band->getId())
但这不可能...
有人能帮忙吗?
如果您已经在 Band 上进行过 Tour,则无需在 Band ID 上过滤 Tour。
所以这会给你这个:
$this->createQueryBuilder('s')
->join('s.band', 'band')
->where('band.id = :bid')
->leftJoin('s.tour', 'tour')
->where('s.day >= :date')
->orderBy('s.day', 'ASC')
->setParameter('date', $date->format('Y-m-d'))
->setParameter('bid', $band->getId())
->getQuery()
->getResult();
有了这个你会得到一个 Show 的 ArrayCollection,如果你在它上面做一个 ->getTour 你可能会有一个 Tour(或 NULL)。
有MEMBER OF
,相信你想要的是这样的:
$this->createQueryBuilder('s')
->leftJoin('s.tour', 'tour')
->where('s.band = :band OR :band MEMBER OF tour.bands')
->setParameter('band', $band)
->andWhere('s.day >= :date')
->setParameter('date', $date->format('Y-m-d'))
->orderBy('s.day', 'ASC')
->getQuery()
->getResult();