集成Zend Framework和Doctrine的问题

时间:2010-11-09 19:05:30

标签: zend-framework command-line doctrine-1.2 doctrine

我之前在另一台机器上完成了这项工作,设置几乎相同。但是,它只是没有在新机器上工作。当我尝试让Doctrine生成我的模型时,它将它们放在错误的位置。它为基类和常规类创建了一个额外的“模型”目录。我正在使用带有ZF 1.11.0的Doctrine 1.2.3。这是我的ZF application.ini:

[production]
phpSettings.display_startup_errors = 0
phpSettings.display_errors = 0
phpSettings.date.timezone = "America/Denver"
includePaths.library = APPLICATION_PATH "/../library"
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
bootstrap.class = "Bootstrap"
appnamespace = ""

resources.frontController.controllerDirectory = APPLICATION_PATH "/controllers"
resources.frontController.params.displayExceptions = 0
resources.layout.layoutPath = APPLICATION_PATH "/layouts/scripts"
resources.view[] = 

autoloaderNamespaces[] = "Doctrine_"
autoloaderNamespaces[] = "App_"

doctrine.connection_string = "mysql://root@localhost/mydb"
;doctrine.cache = true

doctrine.data_fixtures_path = APPLICATION_PATH "/doctrine/data/fixtures"
doctrine.sql_path = APPLICATION_PATH "/doctrine/data/sql"
doctrine.migrations_path = APPLICATION_PATH "/doctrine/migrations"
doctrine.yaml_schema_path = APPLICATION_PATH "/doctrine/schema"
doctrine.models_path = APPLICATION_PATH "/models"

doctrine.generate_models_options.pearStyle = true
doctrine.generate_models_options.generateTableClasses = true
doctrine.generate_models_options.generateBaseClasses = true
doctrine.generate_models_options.baseClassPrefix = "Base_"
doctrine.generate_models_options.baseClassesDirectory =
doctrine.generate_models_options.classPrefixFiles = false
doctrine.generate_models_options.classPrefix = "Model_"

[staging : production]

[testing : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1

[development : production]
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1

这是我的ZF Bootstrap.php文件中的Doctrine函数:

protected function _initDoctrine()
{
    $this->getApplication()->
        getAutoloader()->
        pushAutoloader(array('Doctrine_Core', 'autoload'));

    $config = $this->getOption('doctrine');

    $manager = Doctrine_Manager::getInstance();
    $manager->setAttribute(Doctrine_Core::ATTR_MODEL_CLASS_PREFIX, 'Model_');
    $manager->setAttribute(Doctrine_Core::ATTR_MODEL_LOADING, Doctrine_Core::MODEL_LOADING_PEAR);
    $manager->setAttribute(Doctrine_Core::ATTR_VALIDATE, Doctrine_Core::VALIDATE_ALL);
    $manager->setAttribute(Doctrine_Core::ATTR_USE_DQL_CALLBACKS, true);
    $manager->setAttribute(Doctrine_Core::ATTR_AUTO_FREE_QUERY_OBJECTS, true);
    $manager->setAttribute(Doctrine_Core::ATTR_AUTO_ACCESSOR_OVERRIDE, true);
    $manager->setAttribute(Doctrine_Core::ATTR_AUTOLOAD_TABLE_CLASSES, true);

    if (isset($config['cache']) && $config['cache'] == true) {
        $cacheDriver = new Doctrine_Cache_Apc();
        $manager->setAttribute(Doctrine_Core::ATTR_QUERY_CACHE, $cacheDriver);
    }

    $connection = $manager->openConnection($config['connection_string'], 'doctrine');
    $connection->setAttribute(Doctrine_Core::ATTR_USE_NATIVE_ENUM, true);
    $connection->setCharset('utf8');

    return $connection;
}

这是我的doctrine.php命令行脚本:

// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path()
)));

/** Zend_Application */
require_once 'Zend/Application.php';

// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);

$application->getBootstrap()->bootstrap('doctrine');

$config = $application->getOption('doctrine');
$cli = new Doctrine_Cli($config);
$cli->run($_SERVER['argv']);

所以,我希望我的模型目录最终会像这样结束:

models
  Base
    -Class1.php
    -Class2.php
  -Class1.php
  -Class1Table.php
  -Class2.php
  -Class2Table.php

其中基类类名的格式类似于Model_Base_Class1,常规类名是Model_Class1(在实际的php文件中)。相反,我得到的是:

models   
  Base
    Model
      -Class1.php
      -Class2.php   Model
    -Class1.php
    -Class1Table.php

查看额外的Model目录?是什么造成的?我已经尝试搞乱我的ini中的doctrine.generate_models_options.classPrefix选项以及我的Bootstrap.php文件中的类似设置 - $ manager-> setAttribute(Doctrine_Core :: ATTR_MODEL_CLASS_PREFIX,'Model _');

请帮助我!

1 个答案:

答案 0 :(得分:1)

嗯,我再也不那么聪明了。 :(完全是,我的schema.yaml文件配置错误。我已经将Model_User作为模型名称而不仅仅是用户!虽然夹具文件确实需要它是Model_User。问题解决了。