尝试在zf2 / doctrine下的实体之间创建继承时“未找到类”

时间:2015-03-27 08:44:02

标签: php orm doctrine-orm zend-framework2

我在Zend Framework 2下的项目中有一组带有Doctrine 2注释的实体。 当我尝试使用:

生成模式时
php vendor/bin/doctrine orm:schema-tool:update --force

或当我尝试使用:

生成getter / setter时
php vendor/bin/doctrine orm:generate-entities --update-entities="true" --generate-methods="true" module/Application/src

我总是得到这个错误:

PHP Fatal error:  Class 'Application\Model\Entity\Capteur' not found in /home/...MyApp/module/Application/src/Application/Model/Entity/CapteurResident.php on line 9

这里是完整的堆栈:

PHP Fatal error:  Class 'Application\Model\Entity\Capteur' not found in /home/...MyApp/module/Application/src/Application/Model/Entity/CapteurResident.php on line 9
PHP Stack trace:
PHP   1. {main}() /home/...MyApp/vendor/doctrine/orm/bin/doctrine:0
PHP   2. include() /home/...MyApp/vendor/doctrine/orm/bin/doctrine:4
PHP   3. Doctrine\ORM\Tools\Console\ConsoleRunner::run() /home/...MyApp/vendor/doctrine/orm/bin/doctrine.php:59
PHP   4. Symfony\Component\Console\Application->run() /home/...MyApp/vendor/doctrine/orm/lib/Doctrine/ORM/Tools/Console/ConsoleRunner.php:64
PHP   5. Symfony\Component\Console\Application->doRun() /home/...MyApp/vendor/symfony/console/Symfony/Component/Console/Application.php:126
PHP   6. Symfony\Component\Console\Application->doRunCommand() /home/...MyApp/vendor/symfony/console/Symfony/Component/Console/Application.php:195
PHP   7. Symfony\Component\Console\Command\Command->run() /home/...MyApp/vendor/symfony/console/Symfony/Component/Console/Application.php:874
PHP   8. Doctrine\ORM\Tools\Console\Command\SchemaTool\AbstractCommand->execute() /home/...MyApp/vendor/symfony/console/Symfony/Component/Console/Command/Command.php:257
PHP   9. Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory->getAllMetadata() /home/...MyApp/vendor/doctrine/orm/lib/Doctrine/ORM/Tools/Console/Command/SchemaTool/AbstractCommand.php:59
PHP  10. Doctrine\Common\Persistence\Mapping\Driver\AnnotationDriver->getAllClassNames() /home/...MyApp/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php:113
PHP  11. require_once() /home/...MyApp/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/Driver/AnnotationDriver.php:237

CapteurResident和CapteurEmbarque是两个以完全相同的方式扩展Capteur的实体。

<?php
namespace Application\Model\Entity;
use Doctrine\ORM\Mapping AS ORM;

/**
 * @ORM\Entity
 */
class CapteurResident extends \Application\Model\Entity\Capteur
{

/**
 * @ORM\Entity
 */
class CapteurEmbarque extends \Application\Model\Entity\Capteur
{

如果我删除了实体&#39; CapteurResident&#39;,一切正常!

Entity classes generated to "/home/...MyApp/module/Application/src".

但不是在开发服务器上:在这一个上,我有另一个继承关系的错误...虽然除了数据库参数之外一切都是一样的。 我真的不明白为什么这个bug在一台机器上和另一台机器上出现不同的原因以及为什么有些继承有效,而有些则没有。

PHP Fatal error:  Class 'Application\Model\Entity\Mesure' not found in /home/.../module/Application/src/Application/Model/Entity/MesureCapteurEmbarque.php on line 9

这里是业余班级:

<?php
namespace Application\Model\Entity;
use Doctrine\ORM\Mapping AS ORM;

/**
 * @ORM\Entity
 * @ORM\InheritanceType("SINGLE_TABLE")
 */
class Capteur
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

cli-config.php:

<?php
use Doctrine\ORM\Tools\Console\ConsoleRunner;
require_once 'bootstrap.php';

$entityManager =GetEntityManager ();

return ConsoleRunner::createHelperSet ( $entityManager );

bootstrap.php(除了添加mysql枚举之外没什么特别的):

<?php
require_once "vendor/autoload.php";

use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;

function GetEntityManager() {
    $paths = array (
            'module/Application/src' 
    );
    $isDevMode = true;

    // the connection configuration
    $localConfig = include __DIR__ . '/autoload/local.php';
    $dbParams = $localConfig ['doctrine'] ['connection'] ['orm_default'] ['params'];

    $config = Setup::createAnnotationMetadataConfiguration ( $paths, $isDevMode, "data/proxies", null, false );
    $entityManager = EntityManager::create ( $dbParams, $config );
    $platform = $entityManager->getConnection ()->getDatabasePlatform ();
    $platform->registerDoctrineTypeMapping ( 'enum', 'string' );
    return $entityManager;
}

1 个答案:

答案 0 :(得分:1)

谢谢。我终于找到了一个解决方案:强制Zend自动加载器的行为。但我仍然觉得必须这样做很奇怪。

我在bootstrap.php的开头添加了这些行:

<?php
require_once "vendor/autoload.php";

//insert
Zend\Loader\AutoloaderFactory::factory ( array (
        'Zend\Loader\StandardAutoloader' => array (
                Zend\Loader\StandardAutoloader::LOAD_NS => array (
                        "Application" => __DIR__ . "/../module/Application/src/Application" 
                ) 
        ) 
) );