Before starting, the usual disclaimer : I am aware of dozens of questions here on SE from people encountering identical-looking problems, I have browsed them and unless I missed something, the combination of all the fixes proposed does not solve my particular problem.
In particular :
- This question is about duplicated and inherited entities, which I don't have.
- This answer is about invalid annotation format (missing asterisks), which I don't have in my entity definition (see file content below).
- In this question, the problem
comes from a
@todo
somewhere, which I don't use - In this question, the problem comes from using eAccelerator which I'm not using at this point
I get the following error message in Symfony :
Doctrine\ORM\Mapping\MappingException: Class "AppBundle\Entity\User" is not a valid entity or mapped super class.
Yet, other commands tell me everything is fine :
$ 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
I also tried
try {
$entityManager->getConnection()->connect();
} catch (\Exception $e) {
echo 'Connection failed !';
}
in my code to see if the connection worked. I also tried "registering noop annotation autoloader" as suggested in this SO answer the content of my test file below reflects this ;
<?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);
}
And here is the content of 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;
}
}
Any help appreciated.