1

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.

Community
  • 1
  • 1
Ewan Delanoy
  • 1,276
  • 2
  • 13
  • 31
  • Any misc mapping files under Resources/config/doctrine? They will interfere with your annotations. – Cerad Mar 09 '16 at 12:14
  • @Cerad I don't have any `doctrine` subdirectory inside `vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Resources/config/` – Ewan Delanoy Mar 09 '16 at 12:22
  • Your mappedBy should probably be user, not post. And make sure your add methods call setUser on post and comment. – Cerad Mar 09 '16 at 12:28
  • Just the same error, doctrine CLI doesn't show error, but if I run the website (Slim framework & Doctrine in my case) is not a valid entity. Did you fix it? – Jon Zangitu Jun 07 '18 at 15:50

1 Answers1

1

"user" is a reserved key word in most database systems. But that's why you don't see issues when validating your scheme but get issues later on.

I personally had the case that I was able to even create my schema, but when I used DQL I got some issues.

So you have to avoid or handle the "reserved key word". You have two options:

1)

Rename your class or at least give it a different database table name by:

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

2)

You could also use Doctrine's way to handle reserved keywords (see documentation):

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

But I personally don't recommend the second option.

See as well this section about known limitations in Doctrine around your issue here

Small note: I am assuming that you aren't using the FOS User Bundle - in that case your user would need to extend the BaseUser class additionally.

LBA
  • 3,859
  • 2
  • 21
  • 60