php - Doctrine 2继承映射找不到基表或 View

标签 php symfony doctrine-orm doctrine

我试图使用Docrtrine映射对继承进行建模,但得到一个错误。
我想绘制简单的关系图。UserEntity将是其他用户相关实体(如GuestUserEntityAdminEntity)的基本实体。
我有以下BaseUserEntity的映射

<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                   https://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd">

    <entity name="MngIt\User\Domain\BaseUserEntity"
            repository-class="MngIt\User\Infrastructure\UserEntityRepository"
            inheritance-type="SINGLE_TABLE"
            table="mng_users">

        <id name="id" type="integer" column="id">
            <generator strategy="AUTO"/>
        </id>

        <discriminator-column name="discriminator"/>
        <discriminator-map>
            <discriminator-mapping value="buyer" class="MngIt\Admin\Domain\AdminEntity"/>
        </discriminator-map>

        <field name="login" type="string" column="login"/>
        <field name="username" type="string" column="username"/>
        <field name="email" type="string" column="email"/>
        <field name="balance" type="string" column="balance"/>
        <field name="lang" type="string" column="lang"/>
        <field name="timeDifference" type="string" column="timedifference"/>

    </entity>
</doctrine-mapping>

定义了以下实体类
class BaseUserEntity extends \MngIt\Base\Domain\BaseEntity {

    protected $username;

    protected $login;

    protected $email;

    protected $balance;

    protected $timeDifference;

    /**
     * @return mixed
     */
    public function getUsername() {
        return $this->username;
    }
    // getters ...
}

对于AdminEntity我有以下xml映射
<?xml version="1.0" encoding="utf-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
                   https://raw.github.com/doctrine/doctrine2/master/doctrine-mapping.xsd">

    <entity name="MngIt\Buyer\Domain\BuyerEntity"
            repository-class="MngIt\Admin\Infrastructure\AdminEntityRepository">

    </entity>
</doctrine-mapping>

类的定义如下
<?php

namespace MngIt\Buyer\Domain;

use MngIt\User\Domain\BaseUserEntity;

class AdminEntity extends BaseUserEntity {

}

每当我试图查询用户时,我会得到以下错误
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'mngit.AdminEntity' 

我的配置有什么问题?

最佳答案

首先,我可以假设BaseUserEntity类只是一个包含公共属性的基实体。在这种情况下,最好让这个类abtract

abstract class BaseUserEntity extends \MngIt\Base\Domain\BaseEntity {

我还可以看到,您已经为该实体定义了一个自定义存储库
repository-class="MngIt\User\Infrastructure\UserEntityRepository"只要这是一个基本实体,就不需要定义存储库。
如果配置了缓存,也可以考虑清除缓存。
$config->setQueryCacheImpl(new \Doctrine\Common\Cache\ApcuCache());
$config->setResultCacheImpl(new \Doctrine\Common\Cache\ApcuCache());
$config->setMetadataCacheImpl(new \Doctrine\Common\Cache\ApcuCache());

要清除缓存,可以运行命令
php app/console cache:clear

或编程
$cacheDriver = $entityManager->getConfiguration()->getResultCacheImpl();
$deleted = $cacheDriver->deleteAll();

希望能帮上忙

关于php - Doctrine 2继承映射找不到基表或 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55075124/

相关文章:

php - Codeigniter session 大小限制

php - 如何在 Windows 的交互式 CLI 中运行 php

php - 如何从外部php文件连接joomla3 DB?

PHP 类定义 : Individual accessors/mutators or __set() with switch()?

Symfony2,RabbitMQ : I'm lost

doctrine-orm - Symfony 3.1 Forms with lookup table 和 multiple choices save options (3 tables)

Symfony BinaryFileResponse 切断文件末尾?

php - Doctrine 控制台缺失 : "dest-path"

php - Laravel + Doctrine - 通过 ST_DISTANCE 获得最近的地方

php - Zend_Paginator/Doctrine 2