I am trying to serialize a property which is a Doctrine Criteria:
public function getUserResults(User $user)
{
$criteria = Criteria::create()
->where(Criteria::expr()->eq('user', $user))
;
return $this->getResults()->matching($criteria);
}
I cannot use the @VirtualProperty
because it needs an argument so I implemented a custom subscriber for one of my types following this post:
https://stackoverflow.com/a/44244747
<?php
namespace AppBundle\Serializer;
use JMS\Serializer\EventDispatcher\EventSubscriberInterface;
use JMS\Serializer\EventDispatcher\PreSerializeEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage;
use JMS\Serializer\EventDispatcher\ObjectEvent;
class ExerciseSubscriber implements EventSubscriberInterface
{
private $currentUser;
public function __construct(TokenStorage $tokenStorage)
{
$this->currentUser = $tokenStorage->getToken()->getUser();
}
public static function getSubscribedEvents()
{
return array(
array(
'event' => 'serializer.post_serialize',
'method' => 'onPostSerialize',
'class' => Exercise::class, // if no class, subscribe to every serialization
'format' => 'json', // optional format
),
);
}
public function onPostSerialize(ObjectEvent $event)
{
if (!$this->currentUser) {
return;
}
$exercise = $event->getObject();
$visitor = $event->getVisitor();
$results = $exercise->getUserResults($this->currentUser);
dump($results); // <-- It is an ArrayCollection with many elements
$visitor->setData(
'my_user_results',
$results // <-- when rendered is an empty {}
);
}
}
Unfortunately the user_results
property is always empty!
I looked into the source code for the serializer and I found that:
/**
* Allows you to add additional data to the current object/root element.
* @deprecated use setData instead
* @param string $key
* @param integer|float|boolean|string|array|null $value This value must either be a regular scalar, or an array.
* It must not contain any objects anymore.
*/
public function addData($key, $value)
{
if (isset($this->data[$key])) {
throw new InvalidArgumentException(sprintf('There is already data for "%s".', $key));
}
$this->data[$key] = $value;
}
Please note the It must not contain any objects anymore.
How can I solve this?