Events Doctrine2中实体与其存储库之间通过事件进行通信
我开始在一个项目中使用原则2,该项目的组实体可以从另一个组继承,具有以下模式:id | parent | u id | name 因为层次结构可以深入,所以我使用了一个链接表group_group,使用这个模式:祖先id |后代id |深度 其思想是,任何组都链接到它的所有祖先和后代,深度字段指示关系的距离,这样我就不必使用许多SQL请求遍历父级或子级,单个SQL请求就可以得到所有结果。 我尝试使用条令的多个关系,但我无法按深度字段对其排序,因此我使用实体的存储库来获取相关的祖先和后代 因为一个实体无法访问其存储库,我想知道是否有一种方法可以让一个实体发送其存储库可以监听的事件,以便当一个实体尝试访问其祖先/后代时,存储库可以响应 感谢您的帮助。实体不应该有对存储库的具体引用,但定义接口并让存储库实现此接口并将其注入实体并没有任何错误 类似于此解决方案 然后是你的实体Events Doctrine2中实体与其存储库之间通过事件进行通信,events,entity,doctrine-orm,Events,Entity,Doctrine Orm,我开始在一个项目中使用原则2,该项目的组实体可以从另一个组继承,具有以下模式:id | parent | u id | name 因为层次结构可以深入,所以我使用了一个链接表group_group,使用这个模式:祖先id |后代id |深度 其思想是,任何组都链接到它的所有祖先和后代,深度字段指示关系的距离,这样我就不必使用许多SQL请求遍历父级或子级,单个SQL请求就可以得到所有结果。 我尝试使用条令的多个关系,但我无法按深度字段对其排序,因此我使用实体的存储库来获取相关的祖先和后代 因为一个
class Group
{
$repository;
setRepository(TreeInterface $repository)
{
$this->tree = $repository;
}
public function getParent()
{
return $this->repository->findParent($this);
}
public function getChildren()
{
return $this->repository->findChildren($this);
}
}
class GroupRepository extends EntityRepository implements TreeInterface
{
public function findParent(Group $group)
{
return //stuff
}
public function findChildren(Group $group)
{
return //stuff
}
}
你是这样使用它的
$group = $em->find('Group', 1);
$group->setRepository($em->getRepository('Group'));
$children = $group->getChildren();
为了避免每次获得孩子时都设置存储库,我将查看EventManager和postLoad事件,看看是否可以在加载时将树接口注入实体。实体不应该有对存储库的具体引用,但定义接口并让存储库实现此接口并将其注入实体并没有什么错
类似于此解决方案
然后是你的实体
class Group
{
$repository;
setRepository(TreeInterface $repository)
{
$this->tree = $repository;
}
public function getParent()
{
return $this->repository->findParent($this);
}
public function getChildren()
{
return $this->repository->findChildren($this);
}
}
class GroupRepository extends EntityRepository implements TreeInterface
{
public function findParent(Group $group)
{
return //stuff
}
public function findChildren(Group $group)
{
return //stuff
}
}
你是这样使用它的
$group = $em->find('Group', 1);
$group->setRepository($em->getRepository('Group'));
$children = $group->getChildren();
为了避免每次获得子对象时都设置存储库,我将查看EventManager和postLoad事件,看看是否可以在加载时将树接口注入实体