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

Trait support #1358

Closed
Gabb1995 opened this issue Dec 4, 2019 · 3 comments
Closed

Trait support #1358

Gabb1995 opened this issue Dec 4, 2019 · 3 comments

Comments

@Gabb1995
Copy link

Gabb1995 commented Dec 4, 2019

Description

I use the Gedmo Timestampeable trait in my entities, and would love to expose the createdAt and updatedAt fields by the normalizationContext.

Example

/**
 * @ORM\Entity(repositoryClass="App\Repository\UserRepository")
 * @ApiResource(
 *     collectionOperations={"get", "post"},
 *     itemOperations={"get", "put"},
 *     normalizationContext={"groups"={"user:read"}},
 *     denormalizationContext={"groups"={"user:write"}}
 * )
 * @UniqueEntity(fields={"nickname"})
 * @UniqueEntity(fields={"email"})
 */
class User implements UserInterface
{
    /**
     * @Groups({"user:read"})
     */
    use TimestampableEntity;
/**
 * Timestampable Trait, usable with PHP >= 5.4
 *
 * @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
trait TimestampableEntity
{
    /**
     * @var \DateTime
     * @Gedmo\Timestampable(on="create")
     * @ORM\Column(type="datetime")
     */
    protected $createdAt;

    /**
     * @var \DateTime
     * @Gedmo\Timestampable(on="update")
     * @ORM\Column(type="datetime")
     */
    protected $updatedAt;
@crowincage
Copy link

You need to set groups inside Trait on createdAt and updatedAt properties. Remove Groups on use trait statement. That's the way I use it :-)

@Gabb1995
Copy link
Author

Gabb1995 commented Dec 5, 2019

Yeah after I posted this and was trying to sleep I thought about this, but wasn't sure if it could access groups outside of the same class, guess it can, let me try!

Edit: seems it works like this, thanks will close the issue.

@Gabb1995 Gabb1995 closed this as completed Dec 5, 2019
@kanoamoana
Copy link

kanoamoana commented Apr 9, 2020

Or you could add this to your User entity:

/**
 * @Groups({"user:read"})
 * Returns createdAt.
 * @SerializedName("createdAt")
 */
// createdAT provided by TimestampableEntity trait. 
public function getCreatedAtTimestampable(): ?\DateTimeInterface
{
    return $this->createdAt;
}

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

3 participants