Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JMS Serializer add groups by some condition #646

Closed
shubaivan opened this issue Mar 27, 2018 · 3 comments
Closed

JMS Serializer add groups by some condition #646

shubaivan opened this issue Mar 27, 2018 · 3 comments

Comments

@shubaivan
Copy link

shubaivan commented Mar 27, 2018

I have entity Notification and when my entity contain some condition I want add additional groups for serializer, I create event serializer.pre_serialize for Notification and after check condition add additional groups in serialized context, but in response it's not works. Example I want add some nested to my Notification it would be some another Entity, example Question and this entity have some groups, and after set it when I create context SerializationContext::create() everything work, but when I try add additional group in serializer.pre_serialize it's not work

this my entity

class Notifications
{
use TraitTimestampable;

const GROUP_POST_NOTIFICATION = 'post_notifications';
const GROUP_GET_NOTIFICATIONS = 'get_notifications';

/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 * @Annotation\Groups({
 *     "get_notifications"
 * })
 */
private $id;

/**
 * @var object
 *
 * @Annotation\Groups({
 *     "get_notifications"
 * })
 */
private $providerEntity;

and another entity which I adding in event

class Questions implements NotificationInterface
{
use TraitTimestampable;
const GROUP_POST = 'post_question';
const GROUP_GET = 'get_question';

/**
 * @ORM\Column(type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 * @Annotation\Groups({
 *     "get_question"
 * })
 */
private $id;

    /**
 * @return array
 */
public static function getGetGroup()
{
    return [self::GROUP_GET];
}

but in response I have

public function onPreSerializeNotifications(PreSerializeEvent $event) 
{
    /** @var Notifications $notifications*/
    $notifications = $event->getObject();
    $provider = $notifications->getProvider(); //this is Questions::class
    $this->providerEntity = $this->entityManager->getRepository($provider)
        ->findOneBy(['id' => $notifications->getProviderId()]);
    $notifications->setProviderEntity($this->providerEntity);

    $attr = $event->getContext()->attributes;
    $groups = array_merge($attr->all()['groups'], $provider::getGetGroup());
    $attr->set('groups', $groups);
}

but in response I have

"notifications": [
   {
      "id": 2,
      "provider_entity": {}
   }
]

why ?

@goetas
Copy link
Collaborator

goetas commented Mar 27, 2018

You can not edit the context once the serialization is started. The context should be immutable. In 2.x $attr->set will be removed (to avoid confusion as you are facing)

@goetas goetas closed this as completed Mar 27, 2018
@shubaivan
Copy link
Author

shubaivan commented Mar 27, 2018

@goetas ok, if I correct understand I don't have opportunity add additional group by some condition ?

@goetas
Copy link
Collaborator

goetas commented Mar 27, 2018

Not really, you can use other type of exclusion strategies, see: https://github.com/schmittjoh/serializer/blob/master/doc/cookbook/exclusion_strategies.rst#dynamic-exclusion-strategy as example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants