原则2.1在preUpdate lifeCycleCallback中坚持实体 [英] Doctrine 2.1 Persist entity in preUpdate lifeCycleCallback

查看:110
本文介绍了原则2.1在preUpdate lifeCycleCallback中坚持实体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力下面,在一个实体类中,我有一个preUpdate lifeCycleCallback,它必须持续一个新的实体,然后再刷新一个auditTrail的更改。



在preRemove和prePersist这个功能完美,但在preUpdate没有什么发生。如果我打电话给自己,它会进行递归循环。



根据谷歌组织的原则 - 用户将其放在onFlush应该是一个选项,但在这种情况下,我可以不能访问实体的旧值,以将这个旧值保存在用于audittrail的新的其他实体中。



我想要归档的一些小例子: / p>

 <?php 
/ **
* @Entity
* @HasLifeCycleCallbacks
* /
class someEntity {
...注释...


/ **
* @PreUpdate
*
public function addAuditTrail(){
$ em = \Zend_Registry :: get('doctrine') - > getEntityManager();

$ entity = new AuditTrail();
$ entity-> action ='update';
$ entity-> someField = $ this-> someField;

$ em-> persist($ entity); //这只是不做任何事情:-(
}
}
?>

这不是真正的代码,只是为了说明你想要的东西,我也尝试过这样的一个例子:

  $ em-> getUnitOfWork() - > computeChangeSet($ em-> getClassMetaData(get_class($ entity)),$ entity); 

根据这个主题应该工作: http://groups.google.com/group/doctrine-user/browse_thread/thread/bd9195f04857dcd4



如果我再次打电话但是由于一些无限循环,导致Apache崩溃。



任何有为我取得想法的人,谢谢!

解决方案

您不应该在实体内部使用entitymanager,如果要添加审计跟踪,则应将SomeEntity实体映射到AuditTrail实体,然后执行像

  / ** 
* @PreUpdate
* /
public function addAuditTrail ){
$ entity = new AuditTrail();
$ entity-> action ='update';
$ entity-> someField = $ this-> someField;

$ this-> autitTrail-> add($ entity);
}

如果您在映射中设置了级联选项,那么当您坚持SomeEntity。


I'm struggling with the following, in a entity class I have a preUpdate lifeCycleCallback which has to persist a new entity before it flushes the changes for a auditTrail.

In preRemove and prePersist this works perfectly but in preUpdate nothing happends. If I call flush myself it goes in a recursive loop.

According to the Google groups for doctrine-user putting it in onFlush should be a option but in that event I can't access the old values of the entity to save this old values in a new other entity for the audittrail.

Some small example what i'm trying to archive:

<?php
/**
 * @Entity
 * @HasLifeCycleCallbacks
 */
class someEntity {
    ... annotations ...


    /**
     * @PreUpdate
     */
    public function addAuditTrail() {
        $em = \Zend_Registry::get('doctrine')->getEntityManager();

        $entity = new AuditTrail();
        $entity->action = 'update';
        $entity->someField = $this->someField;

        $em->persist($entity); //this just doesn't do anything :-(
    }
}
?>

It's not real code, just something to illustrate you what I want. I also tried something like this:

$em->getUnitOfWork()->computeChangeSet($em->getClassMetaData(get_class($entity)), $entity);

Which should work according to this topic: http://groups.google.com/group/doctrine-user/browse_thread/thread/bd9195f04857dcd4

If I call the flush again but that causes Apache to crash because of some infinite loop.

Anyone who got ideas for me? Thanks!

解决方案

You should never use the entitymanager inside your entities. If you would like to add audit trails, you should map the "SomeEntity" entity to the "AuditTrail" entity and do something like

/**
 * @PreUpdate
 */
public function addAuditTrail() {
    $entity = new AuditTrail();
    $entity->action = 'update';
    $entity->someField = $this->someField;

    $this->autitTrail->add($entity);
}

If you set the cascade option on the mapping, it will get persisted when you persist "SomeEntity".

这篇关于原则2.1在preUpdate lifeCycleCallback中坚持实体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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