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

serializer doesn't serialize traits #638

Closed
ghost opened this issue Sep 7, 2016 · 8 comments
Closed

serializer doesn't serialize traits #638

ghost opened this issue Sep 7, 2016 · 8 comments

Comments

@ghost
Copy link

ghost commented Sep 7, 2016

Hey,

thanks for this awesome bundle.

I've noticed that serializer doesnt serialize traits. I've this is my entity:

<?php

namespace BusinessBundle\Entity\Acl;

use Symfony\Component\Validator\Constraints as Assert,
    Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

use FOS\UserBundle\Model\User as BaseUser;

use Doctrine\ORM\Mapping as ORM;

use Gedmo\Timestampable\Traits\TimestampableEntity;

use JMS\Serializer\Annotation as Serialize;

/**
 * @Serialize\ExclusionPolicy("all")
 */
class User extends BaseUser
{
    /**
     * Hook timestampable behavior
     */
    use TimestampableEntity;

    /**
     * @var \DateTime $createdAt
     *
     * @Serialize\Expose
     * @Serialize\Type("DateTime")
     * @Serialize\Since("0.15.0")
     * @Serialize\Groups({"acl"})
     */
    protected $createdAt;

    ....
}

When serializing this, I get

{
    "user":{
        "email": "[email protected]",
        "last_login": "2016-09-07T14:04:10+0200",
        "name": "Christian",
        "created_at": null, // it's null :(
        "newsletter": true,
        "ip_address": "192.168.56.1",
        "locale": "de",
        "country": "DE"
    }
}

In the database the value of created_at field is 2016-09-06 21:21:43. Is there anything I'm doing wrong or is it just not possible?

Best
Christian

@goetas
Copy link
Collaborator

goetas commented Sep 7, 2016

In the database the value of created_at field is 2016-09-06 21:21:43.

what is the filed value when you get it using a getter? getCreatedAt() as example

@goetas
Copy link
Collaborator

goetas commented Sep 7, 2016

BTW, https://github.com/Atlantic18/DoctrineExtensions/blob/master/lib/Gedmo/Timestampable/Traits/TimestampableEntity.php already creates the $createdAt property.

Can this block the "Timestampable" behavior? making it null...

@ghost
Copy link
Author

ghost commented Sep 7, 2016

Ahhh, I just was missing an @ORM\Column(type="datetime") annotation at that field. It's working like a charm. Thanks for the help.

@ghost ghost closed this as completed Sep 7, 2016
@ghost
Copy link
Author

ghost commented Sep 7, 2016

Can this block the "Timestampable" behavior?

Yeah, but then I have no possibilities to serialize it via annotations. So I think, I have to override this field.

@goetas
Copy link
Collaborator

goetas commented Sep 7, 2016

I will just not use the trait :)

@ghost
Copy link
Author

ghost commented Sep 7, 2016

Hmm....are there better ways without writing code?

@goetas
Copy link
Collaborator

goetas commented Sep 7, 2016

I mean, I will just copy/paste the content of the trait. Traits are just an "elegant" way of copy/paste

Not easy Re-use is the drawback of annotations.

@ghost
Copy link
Author

ghost commented Sep 7, 2016

Ok, thanks for your advice.

This issue was closed.
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

1 participant