我有一个很大的问题。
1。我有一个FOS的用户类,我有一个Supervisor的注册表格,注册过程非常好。
2。每个主管都有他的交友,他也有一个添加个人的表格,该表格就像主管的注册一样,添加过程在主管类用户的同一类中。
->这样的主管和他的个人都在同一个班级。
现在的问题:
当主管登录时,我将如何选择他的“交友”,并且在同一表中?
意见建议:
1。我在具有2个父级和子级属性的同一个类User中执行ManyToOne和OneToMany,如下所示:
/**
* @ORM\OneToMany(targetEntity="Common\AuthenticationBundle\Entity\User", mappedBy="parent")
*/
protected $childs;
public function __construct() {
$this->childs = new ArrayCollection();
}
/**
* @ORM\ManyToOne(targetEntity="Common\AuthenticationBundle\Entity\User", inversedBy="childs")
* @ORM\JoinColumn(nullable=false)
*/
private $parent;
我不知道这是怎么工作的?
2。我创建了另一个Class SupervisorPersonal,当主管进行新个人添加时,我在其中放置了supervisor_id和personal_id。
这也存在与Doctrine映射的问题,2类和Doctrine如何进行密钥迁移之间有什么关系?
实现此方案的理想方法是在User类上创建多对一关系。多对一关系将代表属于User类的许多个人,这些个人与单个主管(又属于同一User类)具有关系。
在yml config中,可以表示为
manyToOne:
supervisor:
targetEntity: Common\AuthenticationBundle\Entity\User
joinColumn:
name: supervisor_id
referencedColumnName: id
和注解
/**
* @ManyToOne(targetEntity="Common\AuthenticationBundle\Entity\User")
* @JoinColumn(name="supervisor_id", referencedColumnName="id")
**/
private $supervisor;
要获得主管的所有个人信息,您将必须编写一个存储库函数,该函数将supervisorId作为参数。
public function getAllPersonslsForSupervisor($supervisorId)
{
$em = $this->getEntityManager();
$query = $em->createQuery("SELECT e FROM AuthenticationBundle:User e
WHERE e.supervisor = :supervisor")
->setParameter('supervisor', $supervisorId);
$entities = $query->getResult();
return $entities;
}
您可能必须为查询中的实体提供正确的名称空间。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句