MrEvers
MrEvers

Reputation: 1072

Q: Symfony2 - Doctrine - Class xxx is not a valid entity or mapped super class

I have seen many questions in the past about this subject, but none with working solutions. I'm following the Symfony book and cookbook. I used doctrine to fill in getter/setter methods in one of the examples, but it didn't work when I tried to repeat it with another example. When I went back to the previous exercise, it stopped working there too.

The command

php app/console doctrine:generate:entities AppBundle/Entity/User

gives the error

[doctrine\ORM\Mapping\MappingException]
Class "AppBundle\Entity\User" is not a valid entity or mapped super class.

The command

php app/console doctrine:mapping:info

gives the error

[Exception]
You do not have any mapped Doctrine ORM entities according to the current configuration. If you have entities or mapping files you should check your mapping configuration for errors.

This is the class in question:

<?php
namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;

/**
* @ORM\Entity(repositoryClass="AppBundle\Entity\UserRepository")
* @ORM\Table(name="app_users")
*/
class User implements UserInterface, \Serializable {

    /**
    * @ORM\Column(type="integer")
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    */
    private $id;

    /**
    * @ORM\Column(type="string", length=25, unique=true)
    */
    private $username;

    /**
    * @ORM\Column(type="string", length=64)
    */
    private $password;

    /**
    * @ORM\Column(type="string", length=60, unique=true)
    */
    private $email;

    /**
    * @ORM\Column(name="is_active", type="boolean")
    */
    private $isActive;

    public function __construct(){
        $this->isActive = true;
        // may not be needed, see section on salt below
        // $this->salt = md5(uniqid(null, true));
    }

    public function getUsername(){
        return $this->username;
    }

    public function getSalt(){
        // you *may* need a real salt depending on your encoder
        // see section on salt below
        return null;
    }

    public function getPassword(){
        return $this->password;
    }

    public function getRoles(){
        return array('ROLE_USER');
    }

    public function eraseCredentials(){
    }

    /** @see \Serializable::serialize() */
    public function serialize(){
        return serialize(array(
            $this->id,
            $this->username,
            $this->password,
            // see section on salt below
            // $this->salt,
        ));
    }

    /** @see \Serializable::unserialize() */
    public function unserialize($serialized){
        list (
            $this->id,
            $this->username,
            $this->password,
            // see section on salt below
            // $this->salt
        ) = unserialize($serialized);
    }
}

The Doctrine configuration within the config.yml:

    # Doctrine Configuration
doctrine:
    dbal:
        driver:   pdo_mysql
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
        charset:  UTF8
        # if using pdo_sqlite as your database driver:
        #   1. add the path in parameters.yml
        #     e.g. database_path: "%kernel.root_dir%/data/data.db3"
        #   2. Uncomment database_path in parameters.yml.dist
        #   3. Uncomment next line:
        #     path:     "%database_path%"

    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true

My AppKernel.php

    <?php

use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;

class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
            new Symfony\Bundle\SecurityBundle\SecurityBundle(),
            new Symfony\Bundle\TwigBundle\TwigBundle(),
            new Symfony\Bundle\MonologBundle\MonologBundle(),
            new Symfony\Bundle\SwiftmailerBundle\SwiftmailerBundle(),
            new Symfony\Bundle\AsseticBundle\AsseticBundle(),
            new Doctrine\Bundle\DoctrineBundle\DoctrineBundle(),
            new Sensio\Bundle\FrameworkExtraBundle\SensioFrameworkExtraBundle(),
            new AppBundle\AppBundle(),
            new Acme\DemoBundle\AcmeDemoBundle(),
            new Acme\TestBundle\AcmeTestBundle(),
        );

        if (in_array($this->getEnvironment(), array('dev', 'test'))) {
            $bundles[] = new Symfony\Bundle\DebugBundle\DebugBundle();
            $bundles[] = new Symfony\Bundle\WebProfilerBundle\WebProfilerBundle();
            $bundles[] = new Sensio\Bundle\DistributionBundle\SensioDistributionBundle();
            $bundles[] = new Sensio\Bundle\GeneratorBundle\SensioGeneratorBundle();
        }

        return $bundles;
    }

    public function registerContainerConfiguration(LoaderInterface $loader)
    {
        $loader->load($this->getRootDir().'/config/config_'.$this->getEnvironment().'.yml');
    }
}

edit: Additionally, I tried generating a new entity in the same bundle and namespace with Doctrine, which it did without problems. Trying to access that entity again with doctrine:generate:entities (after adding @ORM\Entity and all that) gives me the same error again. So the filename is correct and the namespace is correct...

Upvotes: 0

Views: 2946

Answers (2)

user5208052
user5208052

Reputation: 1

Try:

php app\console doctrine:generate:entity

This will take you to the Doctrine2 entity generator:

This command helps you generate Doctrine2 entities.

Which basically means that you will have to define the table structure for the users database (it needs to "map" the db table with the entity it's creating).

Upvotes: 0

Roberto
Roberto

Reputation: 562

The correct command:

php app/console doctrine:generate:entities AppBundle:User

Upvotes: 1

Related Questions