laravel - 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);
});
}
解决方案
通过为 laravel 学说注册 symfony 注释解决了这个问题更多信息在这里
推荐阅读
- php - PHP:使用静态变量作为方法参数
- stripe-payments - 在之前删除的 Stripe 中创建客户时出现问题
- spreadsheet - 使用 importXML 从 URL 中批量查找售罄的产品页面
- javascript - Mapbox GL JS - 如何添加点云层
- entity-framework - Azure 函数启动中的 EF Core 迁移
- sql - 如何在“Case When Then”SQL 语句中用特殊字符替换字符?
- ios - 一个 viewcontroller 中有几个 tableview,每个 tableview 都有不同的 swipeActions。我该如何管理它们?
- javascript - 为什么 Windows.Forms.WebBrowser 比 iexplore.exe 慢?
- java - 应用程序启动时未触发 Spring Boot Flyway 迁移
- javascript - 如何从辅助文件修改 react useState