【发布时间】:2017-01-05 15:05:33
【问题描述】:
我有三个与各种关系相关联的实体:
-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();
}
当然,这会引发语法错误([Semantical Error] line 0, col 92 near 'bands = :tid': Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected.),因为这些行:
->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())
但这是不可能的……
谁能帮忙?
【问题讨论】:
标签: symfony doctrine-orm dql