Doctrine 2 Lifecyclecallbacks with Abstract Base Class Are Not Called

Doctrine 2 LifecycleCallbacks with abstract base class are not called

Your abstract base class has to be anotated as Mapped Superclasses and include the HasLifecycleCallbacks-Annotation.

Further Information: Inheritance Mapping in the Doctrine Documentation.

/**
* @ORM\MappedSuperclass
* @ORM\HasLifecycleCallbacks
*/
abstract class AbstractBase
{
[...]

/**
* @ORM\PreUpdate
*/
public function setUpdatedAt()
{
$this->updatedAt = new \DateTime();
}
}

/**
* @ORM\Entity(repositoryClass="Entity\Repository\UserRepository")
* @ORM\Table(name="users")
*/
class User extends AbstractBase
{
// some fields, relations and setters/getters defined here, these all work as expected.
}

Doctrine entity callback not called

SO the thing is that I am using the yml file as well as the entity class and it seems as if the annotations doesn't work in parallel to the yml file. I removed the annotations and added the callbacks in the yml file and its working.

Doctrine2 lifecycle callbacks in base class?

Got it working, my mistake.

Here is the solution, if anyone else stumbles on the same issue.

Base class has to have a @MappedSuperclass and @HasLifecycleCallbacks notation.

Entity class, if uses further inheritance, has to be abstract.

http://pastie.org/2661834

HasLifecycleCallbacks on a MappedSuperclass Entity

I implements it on a previous project, the annotation on MappedSuperclass work. The anotation I used is @ORM\HasLifecycleCallbacks() with the parenthesis, and not @ORM\HasLifecycleCallbacks.

This is how I done it (I give all the MappedSuperclass code in a didactic way):

AbstractGenericEntity.php

namespace Services\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\MappedSuperclass
* @ORM\HasLifecycleCallbacks()
*/
class AbstractGenericEntity
{
/**
* @var \DateTime
*
* @ORM\Column(name="Z_CREATION_DATE", type="datetime", nullable=false)
*/
private $dateCreation;

/**
* @var \DateTime
*
* @ORM\Column(name="Z_UPDATE_DATE", type="datetime", nullable=false)
*/
private $dateMaj;

// I just need this getter
public function getUpdateDate($format= 'Y-m-d h:i:s:u') {
return $this->dateMaj->format($format) ;
}

/**
* @return AbstractGenericEntity
* @ORM\PreUpdate
*/
public function preUpdateCallback()
{
$this->dateMaj = new \DateTime("now");
return $this;
}

/**
* @return AbstractGenericEntity
* @ORM\PrePersist
*/
public function prePersistCallback()
{
$this->preUpdateCallback();
$this->dateCreation = new \DateTime("now");
return $this;
}
}

And all my entities inherit of this super-class, without any special other annotation:

namespace Services\Entity;

use Doctrine\ORM\Mapping as ORM;
use Services\Entity\AbstractGenericEntity;

/**
* @ORM\Table(name="MY_TABLE_NAME")
* @ORM\Entity (repositoryClass="Services\Repositories\MyEntityRepository")
*/
class MyEntity extends AbstractGenericEntity
{
public function __construct()
{
$this->id = uniqid('', true);
}

/**
* @var string
*
* @ORM\Column(name="ID", type="string", length=23, nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="NONE")
*
*/
private $id;

// etc, as usual...

Lifecycle Callback Issue When Extending FOSUserBundle User Entity

After a quick look I can only spot a little error with the case of the Annotations name.

It should be

@ORM\PreUpdate

instead of

@ORM\preUpdate

which IMHO should lead to an error when executed.

Anyway I would suggest you to use the DoctrineExtensionsBundle described in http://symfony.com/doc/current/cookbook/doctrine/common_extensions.html .

It comes with a Timestampable (and many more useful) behaviours so you do not need to code this on your own (reinventing the wheel).

I'm using it together with FOSUserBundle and it works fine. This is how my definition in the User Entity looks like:

 /**
* @var \DateTime $created
*
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="datetime")
*/
protected $created;

/**
* @var \DateTime $updated
*
* @Gedmo\Timestampable(on="update")
* @ORM\Column(type="datetime")
*/
protected $updated;

How can I call a initialization method for all my models in doctrine2?

Set your BaseModel to be a mapped superclass with the appropriate life cycle callbacks, eg

/** @MappedSuperclass @HasLifecycleCallbacks */
class BaseModel
{
/** @PostLoad */
public function doStuffOnPostLoad()
{
// do stuff
}
}


Related Topics



Leave a reply



Submit