我有一个实体'员工',它与一个或多个'经理'实体相关联。 因此,我在雇员实体中使用联接表和关联,如下所示:
/**
* @ManyToMany(targetEntity="manager_entity")
* @JoinTable(name="manager_employees",
* joinColumns={@JoinColumn(name="emp_id", referencedColumnName="id")},
* inverseJoinColumns={@JoinColumn(name="manager_id", referencedColumnName="id", unique=true)}
* )
*/
protected $managers;
这已经有效了。但现在我想要检索特定经理的所有员工。 因此,我在问是否有可能做这样的事情:
$mgr = $this->em->getRepository ( 'Entities\manager' )->findOneBy ( array (
"alias" => $this->get('alias'));
// only pseudo code - i know that $managers is a list of managers and $mgr cannot be compared to that
$empList = $this->em->getRepository('Entities\employee')->findBy(array("managers" => $mgr));
答案 0 :(得分:0)
将这样的函数添加到您的存储库:
public function findByManager($managerId)
{
return $this->getEntityManager()
->createQueryBuilder()
->from('employee', 'e')
->innerJoin('e.managers m')
->where('m.Id = :managerId')
->setParameter('managerId', $managerId)
->getQuery()
->getResult();
}
然后使用:
$employee = $repository->findByManager($manager->getId());