在Symfony 2.1中为preUpdate调用添加额外的持久调用 [英] Adding additional persist calls to preUpdate call in Symfony 2.1

查看:115
本文介绍了在Symfony 2.1中为preUpdate调用添加额外的持久调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个preUpdate侦听器。当它被触发时,我希望它创建一些额外的记录。以下是基本功能的简化示例。在这个当前的实现中,似乎新的事件不会被持久化。是否有其他电话我需要在这里做?谢谢。

  public function preUpdate(Event\LifecycleEventArgs $ eventArgs)
{
$ em = $ eventArgs-> getEntityManager();
$ uow = $ em-> getUnitOfWork();
$ entity = $ eventArgs-> getEntity();

$ updateArray = $ eventArgs-> getEntityChangeSet();

//更新
if(($ entity instanceof Bam)=== false){
$ thing = new OtherThing();
$ thing-> setFoo('bar');

$ uow-> persist($ thing);
}

$ uow-> computeChangeSets();
}


解决方案

关键是坚持冲洗后:

 <?php 

命名空间Comakai\CQZBundle\Handler;

使用Symfony\Component\DependencyInjection\ContainerInterface;
使用Doctrine\Common\EventSubscriber;
使用Doctrine\ORM\Event;

/ **
*
* /
class YourHandler实现EventSubscriber
{
protected $ things = [];

public function getSubscribedEvents()
{
/ **
* @todo检查这是否在控制台中运行或什么...
* /
if(isset($ _ SERVER ['HTTP_HOST'])){

return [
'preUpdate',
'postFlush'
];
}

return [];
}

public function preUpdate(Event\LifecycleEventArgs $ eventArgs)
{
$ em = $ eventArgs-> getEntityManager();
$ uow = $ em-> getUnitOfWork();
$ entity = $ eventArgs-> getEntity();

$ updateArray = $ eventArgs-> getEntityChangeSet();

//更新
如果(($ entity instanceof Bam)=== false){

$ thing = new OtherThing();
$ thing-> setFoo('bar');

$ this-> thing [] = $ thing;
}
}

public function postFlush(Event\PostFlushEventArgs $ event)
{
if(!empty($ this-> things) ){

$ em = $ event-> getEntityManager();

foreach($ this->东西为$东西){

$ em-> persist($ thing);
}

$ this-> things = [];
$ em-> flush();
}
}
}


I have a preUpdate listener in my app. When it is fired I want it to create some additional records. A simplified example of the basic functionality is below. In this current implementation it would appear that the new events are not being persisted. Are there other calls I need to be making here? Thanks.

public function preUpdate(Event\LifecycleEventArgs $eventArgs)
{
    $em = $eventArgs->getEntityManager();
    $uow = $em->getUnitOfWork();
    $entity = $eventArgs->getEntity();

    $updateArray = $eventArgs->getEntityChangeSet();

    //Updates
    if (($entity instanceof Bam) === false) {
        $thing = new OtherThing();
        $thing->setFoo('bar');

        $uow->persist($thing);
    }

    $uow->computeChangeSets();
}

解决方案

The key is to persist them after the flush:

<?php

namespace Comakai\CQZBundle\Handler;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event;

/**
 * 
 */
class YourHandler implements EventSubscriber
{
    protected $things = [];

    public function getSubscribedEvents()
    {
        /**
         * @todo Check if this is running in the console or what...
         */
        if (isset($_SERVER['HTTP_HOST'])) {

            return [
                'preUpdate',
                'postFlush'
            ];
        }

        return [];
    }

    public function preUpdate(Event\LifecycleEventArgs $eventArgs)
    {
        $em = $eventArgs->getEntityManager();
        $uow = $em->getUnitOfWork();
        $entity = $eventArgs->getEntity();

        $updateArray = $eventArgs->getEntityChangeSet();

        //Updates
        if (($entity instanceof Bam) === false) {

            $thing = new OtherThing();
            $thing->setFoo('bar');

            $this->things[] = $thing;
        }
    }

    public function postFlush(Event\PostFlushEventArgs $event)
    {
        if(!empty($this->things)) {

            $em = $event->getEntityManager();

            foreach ($this->things as $thing) {

                $em->persist($thing);
            }

            $this->things = [];
            $em->flush();
        }
    }
}

这篇关于在Symfony 2.1中为preUpdate调用添加额外的持久调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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