首页 > 解决方案 > Doctrine Serializer:使用组的问题

问题描述

我正在为我的实体使用 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);
    });
}

标签: laravelsymfonyserializationdoctrine-orm

解决方案


通过为 laravel 学说注册 symfony 注释解决了这个问题更多信息在这里


推荐阅读