【问题标题】:Symfony4 : The annotation does not exist, or could not be auto-loaded (Symfony\Component\Validator\Constraints)Symfony4:注释不存在,或无法自动加载(Symfony\Component\Validator\Constraints)
【发布时间】:2018-05-24 07:54:20
【问题描述】:

我正在开发一个 Symfony4 应用程序,但我遇到了这个错误:

[语义错误]注解 属性中的“@Symfony\Component\Validator\Constraints\NotBlank” App\Entity\Product::$brochure 不存在或不能存在 自动加载。

这里是Productclass:

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity(repositoryClass="App\Repository\ProductRepository")
 */
class Product
{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=100)
     */
    private $name;
    
    /**
     * @ORM\Column(type="decimal", scale=2, nullable=true)
     */
    private $price;

    /**
     * @ORM\Column(type="text")
     */
    private $description;

    /**
     * @ORM\Column(type="string")
     *
     * @Assert\NotBlank(message="Please, upload the product brochure as a PDF file.")
     * @Assert\File(mimeTypes={ "application/pdf" })
     */
    private $brochure;
    
    public function getBrochure()
    {
        return $this->brochure;
    }

    public function setBrochure($brochure)
    {
        $this->brochure = $brochure;

        return $this;
    }

    public function getId()
    {
        return $this->id;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        $this->name = $name;
    }

    // ... getters & setters for price & description

    public function setPrice($price)
    {
        $this->price = $price;
    }

    public function setDescription($description)
    {
        $this->description = $description;
    }
}

注解@Symfony\Component\Validator\Constraints\File也会出错。

也许,我忘了在 Symfony 中配置一些东西,我不知道。

我该如何解决这个问题?

【问题讨论】:

  • 你在哪里称呼它?你什么时候需要?这不是您的代码中缺少use namespace\of\Product 的问题吗?
  • 运行时错误,我猜它是在您放置或发布一些数据时发生的......请确保您传递小册子值并且它不是空白
  • 使用命令php bin/console doctrine:migrations:diff 将代码与数据库同步时出现错误

标签: php symfony annotations symfony4 symfony-validator


【解决方案1】:

也许你应该在 composer 文件中提到,因为在 symfony 4 中,每个依赖项都会通过 flex 自动加载和安装

    "require": {
#...
        "symfony/validator": "^3.3",

    },

【讨论】:

  • 对于 Symfony 4,你可以使用 "symfony/validator": "^4.0" ;)
  • 为我工作...谢谢!
  • 我遇到了同样的问题,但这对我不起作用,因为我已经使用 flex 安装了 symfony\validator(将它添加到 composer.json 清单中)。还有其他想法吗?-
【解决方案2】:

嗯,这是对我来说“我觉得很愚蠢”的修复方法之一。我已经用 flex 安装了这个包,但仍然出现错误。原因是我输入了 symfony 文档中的 Assert 语句示例,但是在我相当高的 DPI 监视器上,反斜杠看起来像一个管道,即: @Assert|NotBlank(),但应该是@Assert\NotBlank()

【讨论】:

  • 呃。你并不孤单。
  • 哇,我现在也觉得自己很蠢。虽然看起来真的很像一个点
猜你喜欢
  • 1970-01-01
  • 2013-05-29
  • 1970-01-01
  • 2017-03-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多