【问题标题】:Symfony4 : The annotation does not exist, or could not be auto-loaded |@Assert\NotBlank()|Symfony4 : 注解不存在,或无法自动加载 |@Assert\NotBlank()|
【发布时间】:2018-03-02 17:23:34
【问题描述】:

当我尝试通过以下方式删除数据库架构时:

$ vendor/bin/doctrine orm:schema-tool:drop --force

我不断收到此错误:

In AnnotationException.php line 54:
[Semantical Error] The annotation 
"@Symfony\Component\Validator\Constraints  
\NotBlank" in property App\Entity\User::$plainPassword does not exist, 
or could not be auto-loaded. 

这是我的 User.php:

<?php

namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;


/**
* @ORM\Table(name="app_users")
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
*/

class User implements UserInterface, \Serializable
{
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\Column(type="string", length=25, unique=true)
 */
private $username;

/**
 * @Assert\NotBlank()
 * @Assert\Length(max=4096)
 */
private $plainPassword;


/** @ORM\Column(type="string", length=64)
 */
private $password;

/**
 * @ORM\Column(type="string", length=60, unique=true)
 */
private $email;

/**
 * @ORM\Column(name="is_active", type="boolean")
 */
private $isActive;

/** @ORM\Column(type="datetime") * */
protected $created_at;

/** @ORM\Column(type="datetime", nullable=true) * */
protected $last_login;

/**
 * @var Video[]
 * @ORM\OneToMany(targetEntity="Video", mappedBy="uploadedBy")
 */
protected $videos;

public function __construct()
{
    $this->isActive = true;
    $this->videos = new ArrayCollection();
}


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

public function getUsername(): ?string //php7.1
{
    return $this->email;
}

public function getSalt() //Not necessary
{
    return null;
}

public function getPlainPassword()
{
    return $this->plainPassword;
}

public function getPassword()
{
    return $this->password;
}

public function getEmail()
{
    return $this->email;
}

public function getRoles()
{
    return array('ROLE_USER');
}

public function eraseCredentials()
{
}

/** @see \Serializable::serialize() */
public function serialize()
{
    return serialize(array(
        $this->id,
        $this->username,
        $this->password,
        $this->isActive,
    ));
}

/** @see \Serializable::unserialize() */
public function unserialize($serialized)
{
    list (
        $this->id,
        $this->username,
        $this->password,
        $this->isActive,
    ) = unserialize($serialized);
}

public function getCreatedAt()
{
    return $this->created_at;
}

public function getLastLogin()
{
    return $this->last_login;
}

public function setUsername(string $username) //php7
{
    $this->username = $username;
}

public function setEmail($email)
{
    $this->email = $email;
}

public function setPlainPassword($password)
{
    $this->plainPassword = $password;
}

public function setPassword($password)
{
    $this->password = $password;
}

public function setCreatedAt($created_at)
{
    $this->created_at = $created_at;
}

public function setLastLogin($last_login)
{
    $this->created_at = $last_login;
}

public function addVideo(Video $video)
{
    if (!$this->hasVideo($video)) {
        $this->videos->add($video);
    }
}

public function deleteVideo(Video $video)
{
    if ($this->hasVideo($video)) {
        $this->videos->removeElement($video);
    }
}

public function hasVideo(Video $video)
{
    return $this->videos->contains($video);
}

public function getVideos()
{
    return $this->videos;
}

public function isAccountNonExpired()
{
    return true;
}

public function isAccountNonLocked()
{
    return true;
}

public function isCredentialsNonExpired()
{
    return true;
}

public function isEnabled()
{
    return $this->isActive;
}
}

我尝试用这种方式解决问题:

Symfony4 : The annotation does not exist, or could not be auto-loaded (Symfony\Component\Validator\Constraints)

这意味着我已经安装了 "symfony/validator": "^4.0"。 但它没有任何建议?

【问题讨论】:

  • 检查 config.yml 文件中是否启用了注解
  • 你指的是哪个文件夹?
  • config.yml 不是原因。您可以复制/粘贴整个实体和 composer.json (需要部分)。你发了composer update 吗?
  • 我上传了整个 User.php,是的,我更新了 compser。
  • 您能否删除该验证规则并查看其他规则是否也导致了问题?

标签: symfony doctrine-orm symfony4 symfony-validator


【解决方案1】:

您的问题是因为您缺少一个注册需要加载的注释的包。

所以你可以安装以下包:

symfony/phpunit-bridge

或者

在你的设置方法中添加这个:

$autoloader = require __DIR__ . '/your_folder_path/vendor/autoload.php';
AnnotationRegistry::registerLoader([$autoloader, 'loadClass']);

【讨论】:

    【解决方案2】:

    检查FrameworkBundle Configuration ("framework") 验证注释已启用。在文件 config/packages/framework.yaml 中查看验证下的选项enable_annotations 正确设置为 true(默认为 false)

    例如:

    framework:
        secret: '%env(APP_SECRET)%'
        validation:      { enable_annotations: true }
    

    希望有帮助

    【讨论】:

    • 验证丢失,但当我添加它时仍然出现同样的错误。
    • 嗨@václav 使用命令 php bin/console debug:config framework 检查实际配置
    • @VáclavStummer 你终于解决了这个问题吗?我正在运行同样的问题。而且我猜想在调用教义cli时没有初始化synfony验证。
    猜你喜欢
    • 2018-05-24
    • 2013-05-29
    • 2017-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    • 1970-01-01
    相关资源
    最近更新 更多