【问题标题】:Doctrine not sure if entity is valid, doctrine:mapping:info contradicts triggered exception学说不确定实体是否有效,学说:映射:信息与触发的异常相矛盾
【发布时间】:2016-06-23 16:15:58
【问题描述】:

在开始之前,通常的免责声明:我知道在 SE 上遇到相同外观问题的人提出了几十个问题,我已经浏览了它们,除非我错过了什么,所有建议的修复组合并不能解决我的特定问题问题。

特别是:

  • 这个question 是关于重复的 和继承的实体,我没有。
  • 这个answer是关于 无效的注释格式(缺少星号),我的实体定义中没有该格式(请参阅下面的文件内容)。
  • 在这个question,问题 来自某个地方的@todo,我不使用它
  • 在这个question,问题 来自使用我目前没有使用的 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 answer 下面我的测试文件的内容反映了这一点;

<?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;
    } 
 }   

任何帮助表示赞赏。

【问题讨论】:

  • Resources/config/doctrine 下是否有其他映射文件?它们会干扰您的注释。
  • @Cerad 我在vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Resources/config/ 中没有任何doctrine 子目录
  • 您的 mappedBy 应该是用户,而不是帖子。并确保您的添加方法在帖子和评论中调用 setUser。
  • 同样的错误,doctrine CLI 不会显示错误,但如果我运行网站(在我的例子中是 Slim 框架和 Doctrine)不是一个有效的实体。你修好了吗?

标签: php symfony doctrine-orm


【解决方案1】:

“user”是大多数数据库系统中的保留关键字。 但这就是为什么您在验证方案时看不到问题但稍后会遇到问题的原因。

我个人的情况是,我什至能够创建我的架构,但是当我使用 DQL 时遇到了一些问题。

所以你必须避免或处理“保留关键字”。你有两个选择:

1)

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

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

2)

你也可以使用 Doctrine 的方式来处理保留关键字(见documentation):

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

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

请参阅此部分,了解 Doctrine 中围绕您的问题 here 的已知限制

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

【讨论】:

  • 谢谢,我还不知道它是否能解决我的问题,但它很有帮助
  • 我很确定它会 - 希望在你向自己证明后得到一个接受的答案;-)
  • 当然。纠正这一点让我发现了我现在正在治疗的其他错误,所以请耐心等待:-)
  • 拥有一个名为 user 的表可能会在以后引起问题,但这与这些 Doctrine 错误无关。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多