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



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

特别是:

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

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

DoctrineORMMappingMappingException: Class "AppBundleEntityUser" is not a valid entity or mapped super class.

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

$ php bin/console doctrine:mapping:info
Found 6 mapped entities:
[OK]   AppBundleEntityCategory
[OK]   AppBundleEntityComment
[OK]   AppBundleEntityPost
[OK]   AppBundleEntitySection
[OK]   AppBundleEntityUser
[OK]   AppBundleEntityWorld

我也试过

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 AppBundleEntity;
use DoctrineORMToolsSetup;
use DoctrineORMEntityManager;
use DoctrineORMMappingDriverAnnotationDriver;
use DoctrineCommonAnnotationsAnnotationReader;
use DoctrineCommonAnnotationsAnnotationRegistry;

$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 AppBundleEntityUser();
  $users[]=$user;
  try { 
     $entityManager->persist($user);
  } catch (Exception $e) {
      var_dump($e);
}   

以下是User.php的内容:

<?php
namespace AppBundleEntity;
use DoctrineORMMapping as ORM;
use SymfonyComponentValidationConstraints as Assert;
/**
* @ORMEntity
* @ORMTable(name="user")
*
**/
class User 
{
    /**
     * @ORMId
     * @ORMGeneratedValue(strategy="AUTO")
     * @ORMColumn(type="integer")
     */
    private $id;
    /**
     * @ORMOneToMany(
     *      targetEntity="Comment",
     *      mappedBy="post",
     *      orphanRemoval=true
     * )
     * 
     */
    private $comments;
    /**
     * @ORMOneToMany(
     *      targetEntity="Post",
     *      mappedBy="post",
     *      orphanRemoval=true
     * )
     * 
     */
    private $posts;
     /**
     * Constructor
     */
    public function __construct()
    {
        $this->comments = new DoctrineCommonCollectionsArrayCollection();
        $this->posts = new DoctrineCommonCollectionsArrayCollection();
    }
    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }
    /**
     * Add comment
     *
     * @param AppBundleEntityComment $comment
     *
     * @return User
     */
    public function addComment(AppBundleEntityComment $comment)
    {
        $this->comments[] = $comment;
        return $this;
    }
    /**
     * Remove comment
     *
     * @param AppBundleEntityComment $comment
     */
    public function removeComment(AppBundleEntityComment $comment)
    {
        $this->comments->removeElement($comment);
    }
    /**
     * Get comments
     *
     * @return DoctrineCommonCollectionsCollection
     */
    public function getComments()
    {
        return $this->comments;
    }
    /**
     * Add post
     *
     * @param AppBundleEntityPosts $post
     *
     * @return User
     */
    public function addPost(AppBundleEntityPosts $post)
    {
        $this->posts[] = $post;
        return $this;
    }
    /**
     * Remove post
     *
     * @param AppBundleEntityPosts $post
     */
    public function removePost(AppBundleEntityPosts $post)
    {
        $this->posts->removeElement($post);
    }
    /**
     * Get posts
     *
     * @return DoctrineCommonCollectionsCollection
     */
    public function getPosts()
    {
        return $this->posts;
    } 
 }   

感谢您的帮助。

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

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

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

1(

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

/**
* @ORMEntity
* @ORMTable(name="myapp_user")
*
**/

2(

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

/**
* @ORMEntity
* @ORMTable(name="'user'")
*
**/

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

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

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

最新更新