Serializing Characteristics Using JMSSerializer

When attempting to serialize a model using traits, the JMSSerializer does not serialize the properties included in this trait. I use yaml to set up the serializer, but it seems like it is not working.

trait IdentityTrait
{

    protected $id;

    public function setId($id)
    {
        $this->id = $id;

        return $this;
    }

    public function getId()
    {
        return $this->id;
    }
}

class OurClass {
   use IdentityTrait;

   protected $test;

   public function getTest() {
       $this->test;
   }
}

Used by JMSSerializerBundle, and the next yaml is in Resources/config/serializer/Model.Traits.IdentityTrait.yml

MyProject\Component\Core\Model\Traits\IdentityTrait:
    exclusion_policy: NONE
    properties:
    id:
        expose: true

And the configuration OurClassis inResources/config/serializer/Model.OurClass.yml

 MyProject\Component\Core\Model\OurClass:
     exclusion_policy: NONE
     properties:
         test:
             expose: true

Some code has been ignored to focus on the problem.

+6
source share
2 answers

PHP , PHP 5.4.0, JMSSerializer PHP 5.3.2. "require": {"php": ">=5.3.2", , (). JMSSerializer github.

+1

Trait:

<?php
namespace AppBundle\Entity;

use JMS\Serializer\Annotation\Expose;
use JMS\Serializer\Annotation\Groups;
use JMS\Serializer\Annotation\Type;


trait EntityDateTrait
{
    /**
     * @var \DateTime
     *
     * @ORM\Column(name="created_at", type="datetime", nullable=true)
     * @Expose()
     * @Groups({"DeploymentListing", "DeploymentDetails"})
     * @Type("DateTime")
     */
    protected $createdAt;

    /**
     *
     * @var \DateTime
     *
     * @ORM\Column(name="updated_at", type="datetime", nullable=true)
     * @Expose()
     * @Groups({"DeploymentListing", "DeploymentDetails"})
     * @Type("DateTime")
     */
    protected $updatedAt;


    /**
     * @ORM\PrePersist()
     *
     * Set createdAt.
     */
    public function setCreatedAt()
    {
        $this->createdAt = new \DateTime();
    }

    /**
     * Get createdAt.
     *
     * @return \DateTime
     */
    public function getCreatedAt()
    {
        return $this->createdAt;
    }

    /**
     * @ORM\PreUpdate()
     *
     * Set updatedAt.
     *
     * @return Campaign
     */
    public function setUpdatedAt()
    {
        $this->updatedAt = new \DateTime();
    }

    /**
     * Get updatedAt.
     *
     * @return \DateTime
     */
    public function getUpdatedAt()
    {
        return $this->updatedAt;
    }
}

@type .

0

All Articles