Consultez la FAQ sur le ZF avant de poster une question
Vous n'êtes pas identifié.
Pages: 1
Bonjour à tous,
je réalise mon premier projet avec ZF et Doctrine. J'ai réussi dans un premier temps à intégrer Doctrine pour le fonctionnement classique en HTTP, et le CLI étant séparé dans un script issu de la sandbox Doctrine. Tout fonctionnait jusqu'au moment où j'ai voulu modifier le schema de Doctrine et donc regénérer mes models et mes tables. Et là plus rien ?!
qques infos au sujet du projet :
* structure modulaire
* Utilisation du MVC
* Bootstrap.php + utilisation des resources (notamment modules)
Après pas mal de recherches (notamment le screencast de zendcasts et celui de Thorin) j'ai réussi à mieux intégrer Doctrine (notamment le CLI dans le bootstrap) et à mieux suivre la convention Zend de nommage des classes.
Du coup mes models sont bien générés à partir du yaml et sont nommés ainsi (pour un module Zend nommé Publications dont le basePath est APPLICATION_PATH . /modules/Publications) :
* Publications_Model_Author (fichier : models/Author.php)
* Publications_Model_Base_Author (fichier models/Base/Author.php)
* Publications_Model_AuthorTable (fichier models/AuthorTable.php)
Voici un tree de l'appli (épuré) :
|-- Bootstrap.php |-- configs | |-- application.ini | `-- doctrine | |-- fixtures | |-- migrations | |-- schema | | |-- schema.yml | | `-- schema.yml.bck | `-- sql | `-- schema.sql |-- controllers |-- forms |-- layouts |-- models |-- modules | `-- Publications | |-- Bootstrap.php | |-- controllers | |-- forms | |-- models | | |-- Author.php | | |-- AuthorTable.php | | |-- Base | | | |-- Author.php | | | |-- BookAuthor.php | | | |-- Book.php | | | |-- Collection.php | | | |-- Currency.php | | | |-- Group.php | | | |-- Identifier.php | | | |-- IdentifierType.php | | | |-- Notice.php | | | |-- NoticeStatus.php | | | |-- Price.php | | | |-- Publisher.php | | | |-- Role.php | | | |-- Status.php | | | |-- UserGroup.php | | | `-- User.php | | |-- BookAuthor.php | | |-- BookAuthorTable.php | | |-- Book.php | | |-- BookTable.php | | |-- Collection.php | | |-- CollectionTable.php | | |-- Currency.php | | |-- CurrencyTable.php | | |-- Group.php | | |-- GroupTable.php | | |-- Identifier.php | | |-- IdentifierTable.php | | |-- IdentifierType.php | | |-- IdentifierTypeTable.php | | |-- Notice.php | | |-- NoticeStatus.php | | |-- NoticeStatusTable.php | | |-- NoticeTable.php | | |-- Price.php | | |-- PriceTable.php | | |-- Publisher.php | | |-- PublisherTable.php | | |-- Role.php | | |-- RoleTable.php | | |-- Status.php | | |-- StatusTable.php | | |-- UserGroup.php | | |-- UserGroupTable.php | | |-- User.php | | `-- UserTable.php | `-- views `-- views
voici une partie de mon application.ini :
[production] appnamespace = "Application" autoloaderNamespaces[] = 'Doctrine_' autoloaderNamespaces[] = 'Local_' doctrine.dsn = "mysql://zf:zf@localhost/zf_publications_local_2?charset=utf8" doctrine.sql_path = APPLICATION_PATH "/configs/doctrine/sql" doctrine.yaml_schema_path = APPLICATION_PATH "/configs/doctrine/schema/schema.yml" doctrine.models_path = APPLICATION_PATH "/modules/Publications/models" doctrine.data_fixtures_path = APPLICATION_PATH "/configs/doctrine/fixtures doctrine.migrations_path = APPLICATION_PATH "/configs/doctrine/migrations 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.classPrefixFiles = false doctrine.generate_models_options.classPrefix = "Publications_Model_" doctrine.generate_models_options.baseClassesDirectory = ; Conservatice model Autoloading doctrine.model_autoloading = 1 [doctrineCLI : production ] ; Agressive model Autoloading doctrine.model_autoloading = 2
Mon pb est que rien ne se passe quand je veux généré du SQL ou créer mes tables ! Je vois bien que c'est un pb d'autoload des models puisque la création des models depuis le YAML fonctionne mais pas tout ce qui se base sur l'instanciation des models (notamment création des tables).
D'après ce que je comprends il est possible de se passer de l'autoload de Doctrine, celui-ci se servant de celui de Zend - et dans le cas d'un module, plus particulièrement d'une instance de Zend_Application_Module_Autoloader. J'ai donc créé une instance de Zend_Application_Module_Autoloader dans mon Bootstrap.php avec comme code :
protected function _initAppLoad() { $moduleLoader = new Zend_Application_Module_Autoloader( array( 'namespace' => 'Publications', 'basePath' => APPLICATION_PATH . '/modules/Publications' ) ); }
et un init de doctrine toujours dans le Bootstrap.php :
protected function _initDoctrine() { /** * Make sure the zend autoloader have access to models * classes */ $this->bootstrap('appload'); $doctrineConfig = $this->getOption('doctrine'); $manager = Doctrine_Manager::getInstance(); $manager->setAttribute(Doctrine_Core::ATTR_MODEL_LOADING, $doctrineConfig['model_autoloading']); $manager->setAttribute(Doctrine_Core::ATTR_AUTO_ACCESSOR_OVERRIDE, true); #$manager->setAttribute(Doctrine_Core::ATTR_MODEL_CLASS_PREFIX, ‘Model_’); $connection = $manager->connection($doctrineConfig['dsn'], 'publications'); $connection->setCharset('UTF8'); $this->getApplication()->getAutoloader()->pushAutoloader(array('Doctrine_Core', 'autoload')); #spl_autoload_register(array('Doctrine_Core', 'modelsAutoload')); Doctrine_Core::setModelsDirectory(realpath(dirname(__FILE__) . '/modules/Publications/models')); Doctrine_Core::loadModels($doctrineConfig['models_path']); return $connection; }
Effectivement je peux bien créér une instance par exemple de Publications_Model_Author dans mon script de CLI :
<?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') : 'doctrineCLI')); // Ensure library/ is on include_path set_include_path(implode(PATH_SEPARATOR, array( realpath(APPLICATION_PATH . '/../library/vendor'), realpath(APPLICATION_PATH . '/../library/vendor/Doctrine'), 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'); /** * Mon autoload fonctionne ! */ $a = new Publications_Model_Author(); $doctrineConfig = $application->getOption('doctrine'); $cli = new Doctrine_Cli($doctrineConfig); $cli->run($_SERVER['argv']);
Par contre le SQL toujours rien !
Une idée ?
Merci (beaucoup) par avance.
Frédéric
Hors ligne
Bon finalement toutes ces lignes pour l'oubli d'une seule !
Si le mécanisme choisi d'autoload des models n'est pas de style PEAR on peut indiquer un préfixe de classe qui sera ajouté au nom du fichier pour obtenir le nom complet de la classe.
Cela se passe dans la méthode Doctrine_Core::loadModels.
Comme ici le modèle de nommage des fichiers n'est pas de style pear les fichiers sont nommés comme le suffixe des classes (Author, Book, etc.). Il fallait donc ajouter la ligne :
$manager->setAttribute(Doctrine_Core::ATTR_MODEL_CLASS_PREFIX, 'Publications_Model_');
plus ajouter ces deux lignes pour charger les models depuis le répertoire du module ZF :
Doctrine_Core::setModelsDirectory(realpath(dirname(__FILE__) . '/modules/Publications/models')); Doctrine_Core::loadModels($doctrineConfig['models_path']);
Frédéric
Hors ligne
Pages: 1