【发布时间】: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