Symfony4:注释不存在,或者无法自动加载| @Assert NotBlank()|

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Symfony4:注释不存在,或者无法自动加载| @Assert NotBlank()|相关的知识,希望对你有一定的参考价值。

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

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

我一直收到这个错误:

In AnnotationException.php line 54:
[Semantical Error] The annotation 
"@SymfonyComponentValidatorConstraints  
NotBlank" in property AppEntityUser::$plainPassword does not exist, 
or could not be auto-loaded. 

这是我的User.php:

<?php

namespace AppEntity;

use DoctrineORMMapping as ORM;
use SymfonyComponentValidatorConstraints as Assert;
use SymfonyBridgeDoctrineValidatorConstraintsUniqueEntity;
use DoctrineCommonCollectionsArrayCollection;
use SymfonyComponentSecurityCoreUserUserInterface;
use SymfonyComponentSecurityCoreUserAdvancedUserInterface;


/**
* @ORMTable(name="app_users")
* @ORMEntity(repositoryClass="AppRepositoryUserRepository")
*/

class User implements UserInterface, Serializable
{
/**
 * @var int
 *
 * @ORMColumn(name="id", type="integer")
 * @ORMId
 * @ORMGeneratedValue(strategy="AUTO")
 */
protected $id;

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

/**
 * @AssertNotBlank()
 * @AssertLength(max=4096)
 */
private $plainPassword;


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

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

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

/** @ORMColumn(type="datetime") * */
protected $created_at;

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

/**
 * @var Video[]
 * @ORMOneToMany(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 (SymfonyComponentValidatorConstraints)

这意味着我已经安装了“symfony / validator”:“^ 4.0”。但它不适用任何sugesstions?

答案

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

例如:

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

希望这有帮助

以上是关于Symfony4:注释不存在,或者无法自动加载| @Assert NotBlank()|的主要内容,如果未能解决你的问题,请参考以下文章

类中的教义注释“@Doctrine\ORM\Annotation\Entity”不存在或无法自动加载

Symfony 4 Bundle:AuthenticationUtils 但不存在这样的服务

无法自动装配服务:参数引用类但不存在此类服务

Symfony 4 不自动装配动态路由控制器

方法中的“@Sensio\Bundle\FrameworkExtraBundle\Configuration\Route”不存在,或无法自动加载”

Symfony4加载类自定义文件夹时出错“预计找到类...但是找不到它”