我正在学习symf4和学说,我对表与表之间的多对多关系有疑问。
我有用户,技能和心情实体。
用户:
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Mood")
* @ORM\JoinTable(
* name="user_mood",
* joinColumns={@ORM\JoinColumn(name="mood_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")}
* )
*/
private $moods;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Skill")
* @ORM\JoinTable(
* name="user_skill",
* joinColumns={@ORM\JoinColumn(name="skill_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")}
* )
*/
private $skills;
在我的数据库中还可以,我有数据透视表等,但是现在我想以某种技能和心情“搜索”所有用户,像这样的有效载荷:
$mood = ['test','test1'];
$skill = ['skillTest'];
我正在尝试使用每个参数中的至少1个来检索所有用户。
这是我的代码:
$qb = $this->em->createQueryBuilder();
$qb->select('u')
->from('App:User', 'u')
->where('u.deletedAt is null')
->andWhere('u.status = :status');
$qb->leftJoin('App:Mood', 'm')
->andWhere('m.title IN (:moodArray)')->setParameter('moodArray', $params['mood']);
$qb->setParameter('status', $status)
->orderBy('u.createdAt', 'DESC')
->setMaxResults($limit)
->setFirstResult($offset);
但是我有这个错误,使用LEFT JOIN
1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE u0_.status = 'ONLINE' AND m1_.title IN ('test' at line 1"
如果我仅使用JOIN,则会检索所有用户,而不是最终目标:b 有什么建议吗?非常重要
编辑: 我以此更改“代码”,结果始终为空数组,始终为LEFT JOIN或JOIN [] U,U
$qb = $this->em->createQueryBuilder();
$qb->select('u')
->from('App:User', 'u')
->where('u.deletedAt is null')
->andWhere('u.status = :status');
$qb->leftJoin('u.moods', 'm')
->andWhere('m.title IN (:moodArray)')->setParameter('moodArray', $params['mood']);
$qb->setParameter('status', $status)
->orderBy('u.createdAt', 'DESC')
->setMaxResults($limit)
->setFirstResult($offset);