FOSUserBundle + JMSSerializerBundle + view additional fields

I am having a problem when serializing an instance Userwith one additional field $namethat extends the base user from FOSUserBundle:

<?php
namespace AppBundle\Entity\User;

use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;

/**
 * User
 */
class User extends BaseUser
{
    /**
     * @var string
     */
    private $name;

    /**
     * Set name
     * @param string $name
     * @return User
     */
    public function setName($name)
    {
        $this->name = $name;

        return $this;
    }

    /**
     * Get name
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }
}

To simplify, I only need to set the field $saltfrom the user object usingJMSSerializerBundle

#AppBundle\Resources\config\serializer\Model.User.yml
FOS\UserBundle\Model\User:
    exclusion_policy: all
    properties:
        salt:
            expose: true

Here's the config for it:

#app\config\config.yml
jms_serializer:
    metadata:
        auto_detection: true
        directories:
            FOSUserBundle:
                namespace_prefix: "FOS\\UserBundle"
                path: "@AppBundle/Resources/config/serializer"

The problem is that the serializer also provides a field $namethat I don't need, since I only need to have $salt:

{
    "salt": "abcdefg",
    "name": "Admin"
}

It seems to me that I need to say that the serializer uses the configuration for mine AppBundle\Entity\Userinstead of the basic user object from FOSUserBundle, but I do not know how to implement it.

+4
2

. User.php, FOS\UserBundle\Model\User BaseUser. BaseUser, .

2 .

config.yml

#Serializer configuration
    jms_serializer:
        metadata:
            directories:
                AppBundle:
                    path: "@AppBundle/Resources/config/serializer"
                FOSUB:
                    namespace_prefix: "FOS\\UserBundle"
                    path: "%kernel.root_dir%/serializer/FOSUB"

Model.User.yml

FOS\UserBundle\Model\User:
    exclusion_policy: ALL
    properties:
        id:
            expose: true
        username:
            expose: true
        email:
            expose: true
        enabled:
            expose: true

Entity.User.yml

AppBundle\Entity\User:
    exclusion_policy: ALL
    properties:
        imageAvatar:
            expose: true
        updatedAt:
            expose: true



namespace AppBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;



/**
 * User
 *
 * @ORM\Table(name="usuario")
 * @ORM\Entity(repositoryClass="DietaBundle\Repository\UserRepository")
 * 
 *
 */
class User extends BaseUser
{
    /**
     * @var int
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;


   /**
     * @ORM\Column(type="string", length=255)
     *
     * @var string
     */
    private $imageAvatar;



    /**
     * @ORM\Column(type="datetime")
     *
     * @var \DateTime
     */
    private $updatedAt;

.

:

+3

, exclusion_policy: all , , .

(config.yml) jms_seriazlier:.

any-name:
    namespace_prefix: "My\\FooBundle"
    path: "@MyFooBundle/Resources/config/serializer"

, .

#AppBundle\Resources\config\serializer\Entity.User.yml
My\FooBundle\Entity\User:
    exclusion_policy: all
    properties:
        salt:
            expose: true
0

All Articles