Serializing Entity Relation only to Id with JMS Serializer

后端 未结 6 554
温柔的废话
温柔的废话 2021-02-02 09:54

I\'m trying to serialize a entity relation with JMS Serializer.

Here is the Entity:

class Ad
{ 

    /**
     * @Type(\"string\")
     * @Groups({\"manag         


        
6条回答
  •  逝去的感伤
    2021-02-02 10:14

    I know this has already been answered but you could also use @Accessor. This probably (may, I can't be sure) work with deserialization too.

    /**
     * @Type("Acme\SearchBundle\Entity\Country")
     * @Groups({"manage"})
     * 
     * @var \Acme\SearchBundle\Entity\Country
     *
     * @Serializer\Accessor(getter="getCountryMinusId",setter="setCountryWithId")
     */
    private $country;
    
    /**
     * @return string|null
     */
    public function getCountryMinusId()
    {
        if (is_array($this->country) && isset($this->country['id'])) {
            return $this->country['id'];
        }
    
        return null;
    }
    
    /**
     * @param string $country
     * @return $this
     */
    public function setCountryWithId($country)
    {
        if (!is_array($this->country)) {
            $this->country = array();
        )
    
        $this->country['id'] = $country;
    
        return $this;
    }
    

提交回复
热议问题