开发者

Path problem with generateModelsFromYaml(..)

开发者 https://www.devze.com 2023-02-14 20:14 出处:网络
What is the good path for schema.yml in build.php->generateModelsFromYaml(..) ? Project paths : /config

What is the good path for schema.yml in build.php->generateModelsFromYaml(..) ?

Project paths :

/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');
?>

And the output of 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 (x开发者_开发技巧86)\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


You should remove first slashes in the line:

Doctrine_Core::generateModelsFromYaml('lib/model/config/schema.yml', 'lib/model/', array('generateTableClasses' => true));

There is my generate.php example:

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');

Note, that it deletes all tables.

It works for me.

0

精彩评论

暂无评论...
验证码 换一张
取 消