条令不确定实体是否有效,条令:映射:信息与触发的异常相矛盾


Doctrine not sure if entity is valid, doctrine:mapping:info contradicts triggered exception

在开始之前,通常的免责声明是:我知道在SE上有几十个问题来自于遇到长相相同问题的人,我已经浏览过了,除非我遗漏了什么,否则所有提出的修复方案的组合并不能解决我的特定问题。

特别是:

  • 这个问题是关于复制和继承的实体,而我没有
  • 这个答案是关于无效的注释格式(缺少星号(,我的实体定义中没有该格式(请参阅下面的文件内容(
  • 在这个问题中来自某个@todo,我不使用它
  • 在这个问题中来自于使用eAccelerator,我目前没有使用它

我在Symfony中收到以下错误消息:

Doctrine'ORM'Mapping'MappingException: Class "AppBundle'Entity'User" is not a valid entity or mapped super class.

然而,其他命令告诉我一切都很好:

$ php bin/console doctrine:mapping:info
Found 6 mapped entities:
[OK]   AppBundle'Entity'Category
[OK]   AppBundle'Entity'Comment
[OK]   AppBundle'Entity'Post
[OK]   AppBundle'Entity'Section
[OK]   AppBundle'Entity'User
[OK]   AppBundle'Entity'World

我也试过

try {
    $entityManager->getConnection()->connect();
} catch ('Exception $e) {
    echo 'Connection failed !';
}

在我的代码中查看连接是否有效。我还尝试过将noop注释自动加载器注册为在此SO答复中建议下面我的测试文件的内容反映了这一点;

<?php
error_reporting(E_ALL|E_STRICT);  
require 'app/autoload.php';
xdebug_break();

use AppBundle'Entity;
use Doctrine'ORM'Tools'Setup;
use Doctrine'ORM'EntityManager;
use Doctrine'ORM'Mapping'Driver'AnnotationDriver;
use Doctrine'Common'Annotations'AnnotationReader;
use Doctrine'Common'Annotations'AnnotationRegistry;

$paths = array("src/AppBundle/Entity");
$isDevMode = true;
// the connection configuration
$dbParams = array(
    'driver'   => 'pdo_mysql',
    'user'     => 'root',
    'password' => 'root',
    'dbname'   => 'asharis_database'
);

$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$driver = new AnnotationDriver(new AnnotationReader(), $paths);
// registering noop annotation autoloader - allow all annotations by default
AnnotationRegistry::registerLoader('class_exists');
$config->setMetadataDriverImpl($driver);

$entityManager = EntityManager::create($dbParams, $config);
try {
    $entityManager->getConnection()->connect();
} catch ('Exception $e) {
    echo 'Connection failed !';
}

$users=array();
$post=array();
for($u=1;$u<=3;$u++) {
  $user=new AppBundle'Entity'User();
  $users[]=$user;
  try { 
     $entityManager->persist($user);
  } catch ('Exception $e) {
      var_dump($e);
}   

以下是User.php的内容:

<?php
namespace AppBundle'Entity;
use Doctrine'ORM'Mapping as ORM;
use Symfony'Component'Validation'Constraints as Assert;
/**
* @ORM'Entity
* @ORM'Table(name="user")
*
**/
class User 
{
    /**
     * @ORM'Id
     * @ORM'GeneratedValue(strategy="AUTO")
     * @ORM'Column(type="integer")
     */
    private $id;
    /**
     * @ORM'OneToMany(
     *      targetEntity="Comment",
     *      mappedBy="post",
     *      orphanRemoval=true
     * )
     * 
     */
    private $comments;
    /**
     * @ORM'OneToMany(
     *      targetEntity="Post",
     *      mappedBy="post",
     *      orphanRemoval=true
     * )
     * 
     */
    private $posts;
     /**
     * Constructor
     */
    public function __construct()
    {
        $this->comments = new 'Doctrine'Common'Collections'ArrayCollection();
        $this->posts = new 'Doctrine'Common'Collections'ArrayCollection();
    }
    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }
    /**
     * Add comment
     *
     * @param 'AppBundle'Entity'Comment $comment
     *
     * @return User
     */
    public function addComment('AppBundle'Entity'Comment $comment)
    {
        $this->comments[] = $comment;
        return $this;
    }
    /**
     * Remove comment
     *
     * @param 'AppBundle'Entity'Comment $comment
     */
    public function removeComment('AppBundle'Entity'Comment $comment)
    {
        $this->comments->removeElement($comment);
    }
    /**
     * Get comments
     *
     * @return 'Doctrine'Common'Collections'Collection
     */
    public function getComments()
    {
        return $this->comments;
    }
    /**
     * Add post
     *
     * @param 'AppBundle'Entity'Posts $post
     *
     * @return User
     */
    public function addPost('AppBundle'Entity'Posts $post)
    {
        $this->posts[] = $post;
        return $this;
    }
    /**
     * Remove post
     *
     * @param 'AppBundle'Entity'Posts $post
     */
    public function removePost('AppBundle'Entity'Posts $post)
    {
        $this->posts->removeElement($post);
    }
    /**
     * Get posts
     *
     * @return 'Doctrine'Common'Collections'Collection
     */
    public function getPosts()
    {
        return $this->posts;
    } 
 }   

感谢您的帮助。

"user"是大多数数据库系统中的保留关键字。但这就是为什么你在验证你的方案时没有看到问题,但稍后会出现问题。

我个人的情况是,我甚至可以创建我的模式,但当我使用DQL时,我遇到了一些问题。

因此,您必须避免或处理"保留关键字"。你有两个选择:

1(

重命名你的类,或者至少给它一个不同的数据库表名:

/**
* @ORM'Entity
* @ORM'Table(name="myapp_user")
*
**/

2(

您也可以使用Doctrine的方式来处理保留关键字(请参阅文档(:

/**
* @ORM'Entity
* @ORM'Table(name="'user'")
*
**/

但我个人并不推荐第二种选择。

请参阅本节关于条令中围绕您的问题的已知限制

小提示:我假设您没有使用FOS用户捆绑包——在这种情况下,您的用户需要额外扩展BaseUser类。