问:Symfony2 - Doctrine - 类xxx不是有效实体或映射超类

时间:2015-10-21 07:48:33

标签: php symfony doctrine-orm doctrine

我过去曾经看过很多关于这个问题的问题,但没有一个问题有工作解决方案。我正在关注Symfony的书和食谱。我在其中一个示例中使用了doctrine来填充getter / setter方法,但是当我尝试用另一个例子重复它时它没有用。当我回到上一次练习时,它也停止在那里工作。

命令

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

给出错误

  

[教义\ ORM \映射\ MappingException]
  类"AppBundle\Entity\User"不是有效实体或映射超类。

命令

php app/console doctrine:mapping:info

给出错误

  

[异常]
  根据当前配置,您没有任何映射的Doctrine ORM实体。如果您有实体或映射文件,则应检查映射配置是否有错误。

这是有问题的课程:

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

config.yml中的Doctrine配置:

    # 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

我的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');
    }
}

修改 另外,我尝试使用Doctrine在同一个bundle和名称空间中生成一个新实体,它没有遇到任何问题。尝试再次使用doctrine:generate:entities(在添加@ORM \ Entity和所有这些之后)再次访问该实体后再次给出了同样的错误。所以文件名是正确的,命名空间是正确的......

2 个答案:

答案 0 :(得分:0)

正确的命令:

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

答案 1 :(得分:0)

尝试:

php app\console doctrine:generate:entity

这将带您进入Doctrine2实体生成器:

  

此命令可帮助您生成Doctrine2实体。

这基本上意味着您必须为用户数据库定义表结构(它需要&#34;映射&#34; db表及其正在创建的实体)。