我正在使用Symfony 5.1 ( PHP7.2 ),问题是实体序列化组注释不起作用。
这是我处理实体并尝试序列化其内容的方式:
/**
* Returns serialized list of articles
*
* @param Article[] $articles
* @return string JSON string
*/
public function serializeArticles(array $articles) : string
{
$dateCallback = function ($innerObject, $outerObject, string $attributeName, string $format = null, array $context = []) {
return $innerObject instanceof \DateTime ? $innerObject->format(\DateTime::ISO8601) : '';
};
$defaultContext = [
AbstractNormalizer::GROUPS => ['user'],
AbstractNormalizer::CALLBACKS => [
'createdAt' => $dateCallback,
'updatedAt' => $dateCallback
],
AbstractNormalizer::CIRCULAR_REFERENCE_HANDLER =>
function ($articles, $format, $context) {
return $articles->getId();
},
AbstractNormalizer::IGNORED_ATTRIBUTES => [
'__initializer__', '__isInitialized__',
'__cloner__'
]
];
$JsonEncoder = new JsonEncoder();
$objectNormalizer =
new ObjectNormalizer(null, null, null,
null, null, null,
$defaultContext)
;
$serializer = new Serializer(
[$objectNormalizer], [$JsonEncoder]
);
$articles = $serializer->serialize($articles, 'json');
return $articles;
}
无论如何,连载小组不想工作。草案看起来我没有使用序列化组。
SMILET