Answer the question
In order to leave comments, you need to log in
Why is JMSSerializerBundle ignoring annotations?
Good evening,
the above gang completely ignores annotations in entities.
For example:
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;
use FOS\UserBundle\Entity\User as BaseUser;
/**
* Модель пользователя
*
* @ORM\Entity
* @ORM\Table(name="user")
*
* @JMS\ExclusionPolicy("all")
*/
class User extends BaseUser
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
* @JMS\Expose
*/
protected $id;
/**
* @ORM\ManyToMany(targetEntity="UserGroup")
* @ORM\JoinTable(name="user_group_relation",
* joinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="group_id", referencedColumnName="id")}
* )
* @JMS\Exclude
*/
protected $groups;
}
jms_serializer:
metadata:
cache: file
debug: "%kernel.debug%"
auto_detection: true
property_naming:
separator: "____"
lower_case: true
[
{
"id": 3,
"username": "Admin",
"username____canonical": "admin",
"email": "ОТРЕЗАНО",
"email____canonical": "ОТРЕЗАНО",
"enabled": true,
"salt": "2ov3jq7rg12cwgck8kgs440wsc4swk8",
"password": "e4de2c084971ef0183ac507bce41ee1d57949de86e94b498f1ba8acc4f192af310282a6981d06e96db2d32e7402fdf0ef4d664c97b034e8eb0d8f0ed81a228e7",
"last____login": "2013-05-17T19:34:25+0400",
"groups": [
{
"id": 1,
"name": "Администратор",
"roles": [
"ROLE_CMS"
]
}
],
"locked": false,
"expired": false,
"roles": [
],
"credentials____expired": false
}
]
Answer the question
In order to leave comments, you need to log in
Through a lot of googling, it turned out that the answer was very close. So why not put it on display, suddenly someone will come in handy.
Due to class inheritance from FOSUserBundle, the annotations did not work.
Here is a treasured topic with a solution: stackoverflow.com/questions/12960141/jmsserializerbundle-no-control-over-third-party-meta-data
You just need to create a yml config for the Model and user from FOSUserBundle.
* Symphony 2.3
$serializer = SerializerBuilder::create()
->setSerializationContextFactory(function () {
return \JMS\Serializer\SerializationContext::create()
->setSerializeNull(true)
;
})
->setDeserializationContextFactory(function () {
return \JMS\Serializer\DeserializationContext::create()
->setSerializeNull(true)
;
})
->setAnnotationReader($annotationReader)
->setPropertyNamingStrategy(new SerializedNameImportantThanPropertyNameStrategy())
->build();
$class = new \ReflectionClass($serializer);
$property = $class->getProperty('navigator');
$property->setAccessible(true);
$navigator = $property->getValue($serializer);
$class = new \ReflectionClass($navigator);
$property = $class->getProperty('metadataFactory');
$property->setAccessible(true);
$metadataFactory = $property->getValue($navigator);
$class = new \ReflectionClass($metadataFactory);
$property = $class->getProperty('loadedClassMetadata');
$property->setAccessible(true);
$property->setValue($metadataFactory, [
'CComponent' => new NullMetadata(new \stdClass()),
'CModel' => new NullMetadata(new \stdClass()),
'CActiveRecord' => new NullMetadata(new \stdClass()),
]);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question