本文介绍了symfony2.8 @ORM \ HasLifecycleCallbacks()无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现@ORM \ PrePersist在我的项目中不起作用,我想在持久保存之前转储一个数字然后终止该过程,但是问题是"$ em-> persist();"仍然执行,并且号码没有转储,这是我的演示:

I found @ORM\PrePersist is not work in my project,I want to dump a number before persist then die the procedure,but the problem is "$em->persist();" still execute,and the number did not dump, here is my demo:

<?php

namespace Nlc\InformationBundle\Entity;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 * @ORM\HasLifecycleCallbacks()
 */

class Adminuser
{
    /**
     * @ORM\PrePersist
     */
    public function test(){
        dump(1);die;
    }
}



<?php

namespace Nlc\InformationBundle\Controller;


use Nlc\InformationBundle\Entity\Adminuser;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;

class AdminuserController extends Controller
{
    /**
     * @Route("/testadmin")
     */
    public function testadmin(){
        $em = $this->getDoctrine()->getManager();
        $adminuser = new Adminuser();
        $adminuser->setUsername('dave');
        $adminuser->setPassword('123');
        //before persist,I want dump 1 then die,but did't work
        $em->persist($adminuser);
        $em->flush();
        return new Response();
    }
}

推荐答案

如果您尝试仅添加一个ID或类似的内容,Doctrine可能会尝试保留您的实体.

If you try to add just an id or something like that maybe Doctrine will try to persist your entity.

<?php

namespace Nlc\InformationBundle\Entity;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 * @ORM\HasLifecycleCallbacks()
 */

class Adminuser
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\PrePersist
     */
    public function test(){
        dump(1);die;
    }
}

这篇关于symfony2.8 @ORM \ HasLifecycleCallbacks()无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 01:30