【问题标题】:Property not exist in edit User API in Symfony 4Symfony 4中的编辑用户API中不存在属性
【发布时间】:2019-06-01 15:37:00
【问题描述】:

我创建了用户实体,这是它的属性之一:

/**
 * @ORM\Column(type="string", length=50, nullable=true)
 *
 * @Assert\Length(max="50")
 * @Assert\NotNull()
 */
private $firstName;

在 Api Controller 中,当我想验证用户或提交表单以供编辑用户时,我会收到错误消息。

控制器:

public function edit(Request $request, ValidatorInterface $validator)
{
    $user = $this->getUser();
    $validator->validate($this->getUser()); # i get error here
}

错误是:

类中不存在属性“firstName” \"代理\__CG__\App\Entity\User\""

但我有 firstName,我可以为此使用 getter 和 setter。

【问题讨论】:

  • 你清除缓存了吗?
  • @Ludo 是并且还使用 rm -fr var/cache/*
  • 使用 $user = new User();但在这种情况下我有错误。
  • 嗯,学说缓存呢? php bin/console doctrine:cache:clear-result php bin/console doctrine:cache:clear-query php bin/console doctrine:cache:clear-metadata 你的缓存文件夹有没有可能不在 var/cache 中?
  • @Ludo 我会测试它。谢谢

标签: api symfony validation doctrine-orm


【解决方案1】:

你可以试试这个方法。

$user = $this->getUser();
$repository = $this->getDoctrine()->getRepository(YourEntity::class);
$userEntity = $repository->find($user->getId());

$validator->validate($userEntity);

【讨论】:

  • 你在使用多用户提供者吗?
  • 不只有一个命名用户
  • 好的,我将再次编辑我的问题并在这里发表评论
猜你喜欢
  • 2014-10-08
  • 1970-01-01
  • 1970-01-01
  • 2018-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多