build.php-> generateModelsFromYaml(..)中schema.yml的好路径是什么?
项目路径:
/config
-global.php
/lib
/model
/config
-schema.yml
/vendor
/doctrine
...
/web
-index.php
-build.php
/config/global.php:
<?php
////////////////////////////////////////////////////////
///////////////////// paths ////////////////////////////
////////////////////////////////////////////////////////
define('LIB_DIR', dirname(__FILE__).'/../lib/');
define('CFG_DIR', dirname(__FILE__).'/');
define('WEB_DIR', dirname(__FILE__).'/../web/');
////////////////////////////////////////////////////////
//////////////////// doctrine conf /////////////////////
////////////////////////////////////////////////////////
define('CFG_DB_DSN', 'mysql://root@localhost/parcvehicule');
require_once(LIB_DIR.'vendor/doctrine/Doctrine.php');
spl_autoload_register(array('Doctrine_Core', 'autoload'));
spl_autoload_register(array('Doctrine_Core', 'modelsAutoload'));
$manager = Doctrine_Manager::getInstance();
$conn = Doctrine_Manager::connection(CFG_DB_DSN, 'doctrine');
$manager->setAttribute(Doctrine_Core::ATTR_VALIDATE, Doctrine_Core::VALIDATE_ALL);
$manager->setAttribute(Doctrine_Core::ATTR_AUTO_ACCESSOR_OVERRIDE, true);
$manager->setAttribute(Doctrine_Core::ATTR_AUTOLOAD_TABLE_CLASSES, true);
$manager->setAttribute(Doctrine_Core::ATTR_MODEL_LOADING, Doctrine_Core::MODEL_LOADING_CONSERVATIVE);
Doctrine_Core::loadModels(LIB_DIR.'model/');
?>
/build.php:
<?php
require_once('config/global.php');
echo $_SERVER["PHP_SELF"];
// Si elle existe, supprimez la base existante.
// Attention, cela vide totalement la base de données !
Doctrine_Core::dropDatabases();
// Création de la base (uniquement si elle n'EXISTE PAS)
Doctrine_Core::createDatabases();
// Création des fichiers de modèle à partir du schema.yml
// Si vous n'utilisez pas le Yaml, n'exécutez pas cette ligne !
Doctrine_Core::generateModelsFromYaml('/lib/model/config/schema.yml', '/lib/model/',
array('generateTableClasses' => true));
// Création des tables
Doctrine_Core::createTablesFromModels('/lib/model');
?>
build.php的输出:
/parcVehicule/build.php
Fatal error: Uncaught exception 'Doctrine_Import_Exception' with message
'No yml schema found in /lib/model/config/schema.yml'
in C:\Program Files (x86)\wamp\www\parcVehicule\lib\vendor\doctrine\Doctrine\Import\Schema.php:277
Stack trace: #0 C:\Program Files (x86)\wamp\www\parcVehicule\lib\vendor\doctrine\Doctrine\Core.php(883):
Doctrine_Import_Schema->importSchema('/lib/model/conf...', 'yml', '/lib/model/')
#1 C:\Program Files (x86)\wamp\www\parcVehicule\build.php(16):
Doctrine_Core::generateModelsFromYaml('/lib/model/conf...', '/lib/model/', Array)
#2 {main} thrown in C:\Program Files (x86)\wamp\www\parcVehicule\lib\vendor\doctrine\Doctrine\Import\Schema.php on line 277
答案 0 :(得分:0)
您应该删除该行中的第一个斜杠:
Doctrine_Core::generateModelsFromYaml('lib/model/config/schema.yml', 'lib/model/', array('generateTableClasses' => true));
有我的generate.php示例:
require_once(dirname(__FILE__) . '/lib/vendor/doctrine/Doctrine.php');
spl_autoload_register(array('Doctrine', 'autoload'));
$manager = Doctrine_Manager::getInstance();
$dsn = 'mysql:dbname=test;host=127.0.0.1';
$user = 'test';
$password = '';
$dbh = new PDO($dsn, $user, $password);
$conn = Doctrine_Manager::connection($dbh);
$conn->setOption('username', $user);
$conn->setOption('password', $password);
$conn->setOption('dsn',$dsn);
$conn->execute('SHOW TABLES');
array('generateTableClasses' => true));
//Doctrine_Core::loadModels('model');
$dbh = Doctrine_Manager::getInstance()->getCurrentConnection()->getDbh();
$dbh->query('DROP TABLE history');
/*
Delete all tables.
*/
unset($dbh);
$options = array(
'generateTableClasses' => true
);
Doctrine_Core::generateModelsFromYaml('config/schema.yml', 'model', $options);
Doctrine_Core::loadModels('model');
Doctrine_Core::createTablesFromModels();
Doctrine_Core::loadData('fixtures');
请注意,它会删除所有表格。
它对我有用。