我有2个具有ManyToMany
关系的实体class User{
/**
* Owning Side
*
* @ORM\ManyToMany(targetEntity="Aire\AppBundle\Entity\Project", inversedBy="projectFollowed")
* @ORM\JoinTable(name="project_followed",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="project_id", referencedColumnName="id")}
* )
**/
private $projectFollowed;
}
class Project{
/**
* Inverse Side
*
* @ORM\ManyToMany(targetEntity="Application\Sonata\UserBundle\Entity\User", mappedBy="projectFollowed")
**/
private $projectFollowed;
}
如果已经有用户分配到此项目,我需要检查我的project_followed表。
$dql = '
SELECT user
FROM Application\Sonata\UserBundle\Entity\User user
INNER JOIN user.projectFollowed project_followed
WHERE project_followed.project_id = :project_id
AND project_followed.user_id = :user_id
';
$em = $this->getDoctrine()->getManager();
$query = $em
->createQuery( $dql )
->setParameter( 'project_id', $project_id )
->setParameter( 'user_id', $user_id )
;
看来我的DQL不正确:
类AppBundle \ Entity \ Project没有名为project_id的字段或关联
所以,如果我改变
WHERE project_followed.project_id = :project_id
到
WHERE project_followed.id = :project_id
它正在工作,但我如何与用户ID建立关系?
这是因为我需要检查是否没有重复的条目。