Mis*_*ope 2 doctrine annotations jmsserializerbundle
我正在尝试运行JMSSerializer.我的简单代码
use JMS\Serializer\Annotation\Type;
class Comment
{
private $msg;
public function __construct($msg)
{
$this->msg = $msg;
}
}
class Person
{
/**
* @Type("array<Comment>")
*/
private $commentList;
public function addComment(Comment $comment)
{
$this->commentList[] = $comment;
}
}
$type = new Type;
$serializer = JMS\Serializer\SerializerBuilder::create()->build();
$data = new Person();
$data->addComment(new Comment('hey'));
var_dump($serializer->serialize($data, 'json'));
Run Code Online (Sandbox Code Playgroud)
失败了
PHP Fatal error: Uncaught exception 'Doctrine\Common\Annotations\AnnotationException' with message '[Semantical Error] The annotation "@JMS\Serializer\Annotation\Type" in property Person::$commentList does not exist, or could not be auto-loaded.' in xxx.php:52
Run Code Online (Sandbox Code Playgroud)
好的,但如果我添加线
$type = new Type;
Run Code Online (Sandbox Code Playgroud)
要手动触发自动加载器,它可以工作:
string(32) "{"comment_list":[{"msg":"hey"}]}"
Run Code Online (Sandbox Code Playgroud)
正如我看到AnnotationRegistry不使用自动加载器,它试图使用一些自己的自动加载器.它看起来很难看,我该怎么做才能解决它?
Mis*_*ope 12
好的,我自己回答了我的问题.我必须在autoloader文件中的某处注册注释:
\Doctrine\Common\Annotations\AnnotationRegistry::registerAutoloadNamespace(
'JMS\Serializer\Annotation', __DIR__.'/vendor/jms/serializer/src'
);
Run Code Online (Sandbox Code Playgroud)
其他方式:http://docs.doctrine-project.org/projects/doctrine-common/en/latest/reference/annotations.html#registering-annotations