Doctrine2 ManyToMany不执行侦听器事件 [英] Doctrine2 ManyToMany doesn't execute listener events

查看:125
本文介绍了Doctrine2 ManyToMany不执行侦听器事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据库结构:

User     > UserRole      < Role
UserId     UserRoleId      RoleId
Name       UserId          Name
           RoleId
           Active
           CreationDate

我的doctrine2类定义如下:

And my doctrine2 classes are defined like this:

/**
 * @var Roles
 *
 * @ORM\ManyToMany(targetEntity="SecRole")
 * @ORM\JoinTable(name="SEC_USER_ROLE",
 *      joinColumns={@ORM\JoinColumn(name="SEC_USER_ID", referencedColumnName="SEC_USER_ID")},
 *      inverseJoinColumns={@ORM\JoinColumn(name="SEC_ROLE_ID", referencedColumnName="SEC_ROLE_ID")}
 *      )
 */
private $userRoles;

public function __construct() {
  parent::__construct();
  $this->userRoles = new \Doctrine\Common\Collections\ArrayCollection();
}

public function addSecRole(\myEntity\SecRole $role)
{
  $exists = $this->userRoles->exists(function($key, $elem) use($role) {
      return isset($elem) && $elem->getSecRoleCode() == $role->getSecRoleCode();
    });
  return !$exists && $this->userRoles->add($role);
}

要向用户添加新角色,请执行以下操作:

To add a new role to the user, I do:

  $r = $rolerep->findOneBySecRoleCode('SystemAdmin');
  $u = $userrep->findOneByUserLogin('sysadmin');
  if (isset($r) && isset($u))
  {
    if ($u->addSecRole($r)) {
      $em->flush();
    }
  }

一切都很好,除非有一件事。生命周期事件不被SecUserRole实体调用!而我的怀疑是,由于Doctrine是为自己添加新的SecUserRole记录,所以它不会调用它的事件。

And everything works fine EXCEPT for one thing. The lifecycle events are not being called for SecUserRole entity!. And my suspicion is that since Doctrine is "adding" the new SecUserRole record for itself, then it doesn't call the events for it.

我正在听prepersist ,preUpdate,preDelete。也没有得到新的记录。我尝试过Flush,但似乎也没有得到它。

I'm listening to prePersist, preUpdate, preDelete. Neither get the new record. I tried onFlush, but it seems it doesn't get it either.

有没有什么我错过了,我该如何解决?自己做插件?当然这是一个解决方案,但这让我也自己做这个问题,这是我不想做的。

Is there something I'm missing, how could I solve this? doing the inserts by myself? Sure that's a solution, but that leaves me to do also the queries myself, which is something I don't want to do.

提前谢谢
KAT LIM

Well, thanks in advance KAT LIM

推荐答案

到目前为止,还没有找到最好的方法但似乎Doctrine假定你的加入表将是自动生成,所以它假设它没有也不需要比两个加入密钥(UserId,RoleId)更多。

So far, haven't found the best way BUT seems that Doctrine assumes that your Join Table will be "autogenerated" so it assumes that it doesn't have nor need more than the two joining keys (UserId, RoleId).

我做了什么来解决它是停止使用一个ManyToMany,但使用OneToMany关系到SecUserRole表。所以在 addSecRole 方法中,我插入了新的对象,然后在外面刷新EM(如上面的例子)。

What I did to solve it was to stop using a ManyToMany, but use a OneToMany relationship to SecUserRole table. So inside the addSecRole method, I inserted the new object and then flushed the EM on the outside (like the example above).

那是我最好的办法。我得到了这个 http://www.zendcasts.com/ 的想法,那里有一个专为ManyToMany

It seems that's the best way I could do. I got the idea from this http://www.zendcasts.com/ where there is one cast specially for ManyToMany mappings.

嗯,希望这有助于所有的

Well, hope this helps to all

这篇关于Doctrine2 ManyToMany不执行侦听器事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆