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

getter is not called when serializing Discriminator parent entity #156

Closed
nicolaslattuada opened this issue Sep 18, 2013 · 4 comments
Closed

Comments

@nicolaslattuada
Copy link

Hello
It seems that at the moment when using Discriminator, the getter for exposed property does not get called.
I am adding a code example to explain my question.

/**
 * @Serializer\Discriminator(field = "type", map = {
 *    "car": "JMS\Serializer\Tests\Fixtures\Discriminator\Car",
 *    "moped": "JMS\Serializer\Tests\Fixtures\Discriminator\Moped",
 * })
 */
abstract class Vehicle
{
    /** @Serializer\Type("integer") */
    public $km;

    public function getKm()
    {
        return 25;
    }
}

Given this code snippet, would it be possible to have serialized property km to have the value of 25?
Thanks in advance!
Nicolas.

@stof
Copy link
Contributor

stof commented Sep 22, 2013

What you need is the @AccessType annotation to make the serializer use the public methods rather than Reflection

@nicolaslattuada
Copy link
Author

ok, thanks a lot :-)

@gustawdaniel
Copy link

gustawdaniel commented Apr 22, 2018

I applied AccessType annotation @stof and it works.

I want use /** @AccessType("public_method") */ globally.

Is it possible to configure it in one place, and do not repeat in any Entity or Document?

And I do not want to apply it to setters, but only for getters, because I do not have setters on some properties, and it throws error:

There is no public setCreatedAt method in class App\Document\User. Please specify which public method should be used for setting the value of the property createdAt.

@Namoshek
Copy link
Contributor

Namoshek commented Sep 3, 2018

For future reference, there is a hint regarding this issue in the docs:

Note: If you need only to serialize your data, you can avoid providing a setter by setting the property as read-only using the @ReadOnly annotation.

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

4 participants