symfony - FOSUserBundle + JMSSerializerBundle + exposing extra fields -
i have issue when serializing user
instance 1 additional field $name
, extends base user fosuserbundle:
<?php namespace appbundle\entity\user; use doctrine\orm\mapping orm; use fos\userbundle\model\user 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; } /** * name * @return string */ public function getname() { return $this->name; } }
to simplify, need expose $salt
field user entity using jmsserializerbundle
#appbundle\resources\config\serializer\model.user.yml fos\userbundle\model\user: exclusion_policy: properties: salt: expose: true
here's config it:
#app\config\config.yml jms_serializer: metadata: auto_detection: true directories: fosuserbundle: namespace_prefix: "fos\\userbundle" path: "@appbundle/resources/config/serializer"
the issue is serializer exposes $name
field, don't want need have $salt
exposed:
{ "salt": "abcdefg", "name": "admin" }
i believe need tell serializer use config appbundle\entity\user
instead of base user entity fosuserbundle, have no clue how implement it.
this how solved issue . have user.php class inherit fos\userbundle\model\user baseuser. need control serialization both baseuser class , user class.
solution: need 2 separated config file control each class.
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: properties: id: expose: true username: expose: true email: expose: true enabled: expose: true
entity.user.yml
appbundle\entity\user: exclusion_policy: properties: imageavatar: expose: true updatedat: expose: true namespace appbundle\entity; use fos\userbundle\model\user baseuser; use doctrine\orm\mapping 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;
clear cache after each change in configuration files.
cache:clear
Comments
Post a Comment