【问题标题】:Q: Symfony2 - Doctrine - Class xxx is not a valid entity or mapped super class问:Symfony2 - Doctrine - Class xxx 不是有效的实体或映射的超类
【发布时间】:2016-01-20 02:51:33
【问题描述】:

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

命令

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

给出错误

[学说\ORM\Mapping\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 在同一个包和命名空间中生成一个新实体,它没有问题。尝试使用教义再次访问该实体:generate:entities (在添加 @ORM\Entity 和所有这些之后)再次给我同样的错误。所以文件名正确,命名空间正确...

【问题讨论】:

  • 这两个错误表明问题是Doctrine无法找到您的映射,我不知道为什么。正如消息所示,您需要检查映射配置。你有在 Yaml 或 XML 中定义的映射吗?
  • 看看这个关于映射配置的信息:symfony.com/doc/current/reference/configuration/doctrine.html
  • 在哪里可以找到这个? Doctrine 在这里仅用作 Symfony 的工具,但没有在课程中记录。
  • 没有具体配置,只有auto_mapping: true
  • 这也可能是由于命名空间或文件名不正确..

标签: php symfony doctrine-orm doctrine


【解决方案1】:

试试:

php app\console doctrine:generate:entity

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

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

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

【讨论】:

    【解决方案2】:

    正确的命令:

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

    【讨论】:

    • 我在我的项目中使用了你的命令,但我没有工作,对不起。
    猜你喜欢
    • 2016-09-09
    • 2013-02-05
    • 2014-02-21
    • 1970-01-01
    • 1970-01-01
    • 2011-12-21
    • 2018-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多