Symfony - prePersist

时间:2015-03-18 18:55:06

标签: symfony doctrine-orm persist listeners

prePersist没有被解雇。

    $manager = $this->getDoctrine()->getManager();

    $gateway =  /* ... some logic here ... */

    $person = new User();
    $person->setName('Bruno');
    $person->setEmail('bruno_nonato_quaresma@hotmail.com');
    $person->setPassword(md5('some-one'));       
    $person->setMembersArea($gateway->getProject()->getMembersArea());
    $person->setActive(true);  

    $manager->persist($person);
    $manager->flush();

我的config.yml

 mymembers.listener.generateUsername:
    class: MYmembers\AdminBundle\EventListener\UsernameGenerator
    tags:
        - { name: doctrine.event_listener, event: prePersist }

UsernameGenerator类方法

public function prePersist(LifecycleEventArgs $args)
{
    $entity = $args->getEntity();
    $entityManager = $args->getEntityManager();

    if ($entity instanceof User) 
    {
        $this->setUsername($entity, $entityManager);    
    }
}

奇怪的是,在夹具中调用了UsernameGenerator,但是当我在控制器中执行SAME代码时,它不起作用...... :(

0 个答案:

没有答案