Doctrine Serializer:使用组的问题
Doctrine Serializer : Problem on using groups
我正在为我的实体使用 symfony 序列化程序,没有问题,除非我尝试在我的实体属性上使用组时出现此错误:
[Semantical Error] The annotation "@Symfony\Component\Serializer\Annotation\Groups" in property
App\Entities\User::$id was never imported. Did you maybe forget to add a "use" statement for this
annotation?
这是我的实体:
namespace App\Entities;
use Doctrine\ORM\Mapping AS ORM;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ORM\Entity
*/
class User
{
/**
* @ORM\Id
* @Groups("user_show")
* @ORM\Column(type="string")
*/
protected $id;
/**
* @ORM\Column(type="string")
*/
protected $password;
}
我的 AppServiceProvider
中有此代码(我正在使用 Laravel)
public function boot()
{
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$encoders = [new JsonEncoder()];
$normalizers = [new ObjectNormalizer($classMetadataFactory)];
$this->app->bind(SerializerInterface::class, function () use ($normalizers, $encoders) {
return new Serializer($normalizers, $encoders);
});
}
通过为 laravel 学说注册 symfony 注释解决了这个问题
更多信息 here
我正在为我的实体使用 symfony 序列化程序,没有问题,除非我尝试在我的实体属性上使用组时出现此错误:
[Semantical Error] The annotation "@Symfony\Component\Serializer\Annotation\Groups" in property
App\Entities\User::$id was never imported. Did you maybe forget to add a "use" statement for this
annotation?
这是我的实体:
namespace App\Entities;
use Doctrine\ORM\Mapping AS ORM;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ORM\Entity
*/
class User
{
/**
* @ORM\Id
* @Groups("user_show")
* @ORM\Column(type="string")
*/
protected $id;
/**
* @ORM\Column(type="string")
*/
protected $password;
}
我的 AppServiceProvider
中有此代码(我正在使用 Laravel)
public function boot()
{
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$encoders = [new JsonEncoder()];
$normalizers = [new ObjectNormalizer($classMetadataFactory)];
$this->app->bind(SerializerInterface::class, function () use ($normalizers, $encoders) {
return new Serializer($normalizers, $encoders);
});
}
通过为 laravel 学说注册 symfony 注释解决了这个问题 更多信息 here