我正在使用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;}无论如何,连载小组不想工作。草案看起来我没有使用序列化组。
1 回答
SMILET
TA贡献1796条经验 获得超4个赞
看来您没有将AnnotationsReader对象传递给 ObjectNormalizer 的第一个参数。您应该更仔细地阅读文档。
use Doctrine\Common\Annotations\AnnotationReader;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
我认为这应该有帮助:)
- 1 回答
- 0 关注
- 76 浏览
添加回答
举报
0/150
提交
取消