I have to log the changes of each entity. I've Listener which listens for doctrine's events on preRemove
, postUpdate
and postDelete
.
My entity AccessModule
has relations:
App\Entity\AccessModule.php
/**
* @ORM\OneToMany(targetEntity="App\Entity\AccessModule", mappedBy="parent")
* @ORM\OrderBy({"id" = "ASC"})
*/
private $children;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\AccessModule", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id", nullable=true)
*/
private $parent;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\AccessModuleRoute", inversedBy="access_modules")
* @ORM\JoinTable(name="access_routes",
* joinColumns={@ORM\JoinColumn(name="access_module_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="route_id", referencedColumnName="id")})
*
*/
private $routes;
in listener: App\EventListener\EntityListener.php
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
$encoders = [new XmlEncoder(), new JsonEncoder()];
$normalizer = new ObjectNormalizer();
$normalizer->setCircularReferenceHandler(function ($object) {
return $object->getId();
});
$this->serializer = new Serializer([$normalizer], $encoders);
public function createLog(LifecycleEventArgs $args, $action){
$em = $args->getEntityManager();
$entity = $args->getEntity();
if ($this->tokenStorage->getToken()->getUser()) {
$username = $this->tokenStorage->getToken()->getUser()->getUsername();
} else {
$username = 'anon'; // TODO Remove anon. set null value
}
$log = new Log();
// $log->setData('dddd')
$log->setData($this->serializer->serialize($entity, ''json)
->setAction($action)
->setActionTime(new \DateTime())
->setUser($username)
->setEntityClass(get_class($entity));
$em->persist($log);
$em->flush();
}
I've problem with serialization
When I use $log->setData($entity)
I get problem with Circular.
Whan I do serialization $log->setData($this->serializer->serialize($entity, ''json)
I get full of relations, with parent's children, with children children. In a result I get full tree :/
I'd like to get
Expect
[
'id' => ID,
'name' => NAME,
'parent' => parent_id // ManyToOne, I'd like get its id
'children' => [$child_id, $child_id, $child_id] // array of $id of children array collection
]
(ofcourse this is draft before encode it to json)
How can I get expected data without full relations?
The Symfony Serializer component is a very powerful and useful component when writing an API. It handles the conversion between your request data (JSON, XML, anything really…) and your classes (usually your entities). If you're not familiar with the Symfony Serializer component, please read the documentation first.
In essence, a repository is a collection of entity objects. In the following example, we work with the City entity. We create a new Symfony skeleton project and locate to the newly created project directory. We work with Symfony 5.0.8 version.
It is configured by default in Symfony applications with the Serializer component enabled. This normalizer reads the content of the class by calling the "getters" (public methods starting with "get"). It will denormalize data by calling the constructor and the "setters" (public methods starting with "set").
Symfony was inspired by Django, RoR, and Spring frameworks. An entity is a lightweight domain object which is to be persisted. Typically, an entity represents a table in a relational database, and each entity instance corresponds to a row in the table.
Thing you are looking for is called serialization groups: here and here.
Now let me explain how it works. It's quite simple. Say you have Post Entity:
class Post
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"default"})
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User\User")
* @Groups({"default"})
*/
private $author;
}
And you have also User Entity:
class User
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Groups({"default"})
*/
private $id;
/**
* @ORM\Column(type="string", length=40)
* @Groups({"default"})
*/
private $firstName;
/**
* @ORM\Column(type="string", length=40)
*/
private $lastName;
}
Post can have author(User) but I don't want to return all User data every single time. I'm interested only in id and first name.
Take a closer look at @Groups annotation. You can specify so called serialization groups. It's nothing more than convinient way of telling Symfony which data you would like to have in your result set.
You have to tell Symfony serializer which relationships you would like to keep by adding relevant groups in form of annotation above property/getter. You also have to specify which properties or getters of your relationships you would like to keep.
Now how to let Symfony know about that stuff?
When you prepare/configure your serializaition service you just simply have to provide defined groups like that:
return $this->serializer->serialize($data, 'json', ['groups' => ['default']]);
It's good to build some kind of wrapper service around native symfony serializer so you can simplify the whole process and make it more reusable.
Also make sure that serializer is correctly configured - otherwise it will not take these group into account.
That is also one way(among other ways) of "handling" circular references.
Now you just need to work on how you will format your result set.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With