DoctrineExtensions
DoctrineExtensions copied to clipboard
[Loggable] Loggable entity is instantiated without constructor
I am using an own loggable entity which contains a one to many relation for the changed data instead of writing the data as serialized array in a text column. As you all know doctrine one to many relations are stored in a collection, which is instantiated in the constructor of the entity.
<?php
declare(strict_types=1);
namespace Marcel\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
...
#[ORM\OneToMany(targetEntity: HistoryValue::class, mappedBy: "parent", cascade: [ "persist", "remove" ])]
protected Collection $data;
public function __construct()
{
$this->data = new ArrayCollection();
}
...
In \Gedmo\Loggable\LoggableListener::createLogEntry
in line 296 a new history entity is created via metadata s instantiator. The instantiator creates a new entity instance via reflection without executing the constructor. In the shown case the data property will never be instantiated as ArrayCollection
. That makes it difficult to use normalized, individual entity classes for loggable entries.
A possible solution could be instantiating the entities directly with the new
keyword or via ReflectionClass::newInstance()
. This would avoid follow up exceptions when persisting relations in a loggable entity.