2010-12-03 5 views
2

J'essaie actuellement d'intégrer la doctrine ORM 2 avec Zend Framework. J'essaie d'utiliser le XMLDriver. Tout fonctionne bien jusqu'à ce que j'essaie de générer le schéma. En effet, les entités sont bien créées.Zend 1.10/Doctrine 2, impossible de traiter le schéma avec mappage XML

Donc, voici le fichier bootstrap:

<?php 

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap { 

/** 
* generate registry 
* @return Zend_Registry 
*/ 
protected function _initRegistry() { 
    $registry = Zend_Registry::getInstance(); 
    return $registry; 
} 

/** 
* Register namespace App_ 
* @return Zend_Application_Module_Autoloader 
*/ 
protected function _initAutoload() { 
    $autoloader = new Zend_Application_Module_Autoloader(array(
       'namespace' => '', 
       'basePath' => dirname(__FILE__), 
      )); 

    new Doctrine\Common\ClassLoader('Application', APPLICATION_PATH); 

    return $autoloader; 


} 

/** 
* Initialize auto loader of Doctrine 
* 
* @return Doctrine_Manager 
*/ 
function _initDoctrine() { 
    // setup Zend & Doctrine Autoloaders 
    require_once "Doctrine/Common/ClassLoader.php"; 

    $zendAutoloader = Zend_Loader_Autoloader::getInstance(); 

    // $autoloader = array(new \Doctrine\Common\ClassLoader(), 'loadClass'); 

    $autoloader = array(new \Doctrine\Common\ClassLoader('Symfony'), 'loadClass'); 
    $zendAutoloader->pushAutoloader($autoloader, 'Symfony\\'); 
    $autoloader = array(new \Doctrine\Common\ClassLoader('Doctrine'), 'loadClass'); 
    $zendAutoloader->pushAutoloader($autoloader, 'Doctrine\\'); 
    $autoloader = array(new \Doctrine\Common\ClassLoader('DoctrineExtensions'), 'loadClass'); 
    $zendAutoloader->pushAutoloader($autoloader, 'DoctrineExtensions\\'); 
    $autoloader = array(new \Doctrine\Common\ClassLoader('Application\\Models', realpath(__DIR__ . '/..')), 'loadClass'); 
    $zendAutoloader->pushAutoloader($autoloader, 'Application\\Models\\'); 
    $autoloader = array(new \Doctrine\Common\ClassLoader('Application\\Proxies', realpath(__DIR__ . '/..')), 'loadClass'); 
    $zendAutoloader->pushAutoloader($autoloader, 'Application\\Proxies'); 
    $autoloader = array(new \Doctrine\Common\ClassLoader('DoctrineExtensions'), 'loadClass'); 
    $zendAutoloader->pushAutoloader($autoloader, 'DoctrineExtensions\\'); 

    // setup configuration as seen from the sandbox application 
    // TODO: read configuration from application.ini 
    $config = new \Doctrine\ORM\Configuration; 
    $cache = new \Doctrine\Common\Cache\ArrayCache; 
    $config->setMetadataCacheImpl($cache); 
    //$driverImpl = $config->newDefaultAnnotationDriver(realpath(__DIR__ . '/models')); 
    $driverImpl = new \Doctrine\ORM\Mapping\Driver\XmlDriver(array(APPLICATION_PATH . '/models/entities/mapping')); 
    $driverImpl->setFileExtension('.xml'); 
    $config->setMetadataDriverImpl($driverImpl); 
    $config->setQueryCacheImpl($cache); 
    $config->setProxyDir(APPLICATION_PATH . '/models/proxies'); 
    $config->setProxyNamespace('Application\\Proxies'); 
    $config->setAutoGenerateProxyClasses(true); 

    $doctrineConfig = $this->getOption('doctrine'); 
    $connectionOptions = array(
     'driver' => $doctrineConfig['connection']['driver'], 
     'host' => $doctrineConfig['connection']['host'], 
     'port' => $doctrineConfig['connection']['port'], 
     'user' => $doctrineConfig['connection']['user'], 
     'password' => $doctrineConfig['connection']['password'], 
     'dbname' => $doctrineConfig['connection']['dbname'] 
    ); 

    // setup entity manager 
    $em = \Doctrine\ORM\EntityManager::create($connectionOptions, $config); 
    Zend_Registry::set("entitymanager", $em); 
    return $em; 
} 

}

est enfin ici le fichier doctrine.php:

<?php 

ob_start(); 

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

// Define application environment 
define('APPLICATION_ENV', 'development'); 

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

require_once 'Doctrine/Common/ClassLoader.php'; 
$classLoader = new \Doctrine\Common\ClassLoader('Doctrine', APPLICATION_PATH . '/../library'); 
$classLoader->register(); 
$classLoader = new \Doctrine\Common\ClassLoader('Symfony', APPLICATION_PATH . '/../library/Doctrine'); 
$classLoader->register(); 
$classLoader = new \Doctrine\Common\ClassLoader('Entities', APPLICATION_PATH . '/models/entities'); 
$classLoader->setNamespaceSeparator('_'); 
$classLoader->register(); 

// Create application, bootstrap 
/** Zend_Application */ 
require_once 'Zend/Application.php'; 
$application = new Zend_Application(
       APPLICATION_ENV, 
       APPLICATION_PATH . '/configs/application.ini' 
); 

$application->bootstrap(); 
$em = $application->getBootstrap()->getResource('doctrine'); 

$helperSet = new \Symfony\Component\Console\Helper\HelperSet(array(
      'db' => new \Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper($em->getConnection()), 
      'em' => new \Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper($em) 
     )); 

$helperSet = ($helperSet) ? : new \Symfony\Component\Console\Helper\HelperSet(); 

$cli = new \Symfony\Component\Console\Application('Doctrine Command Line Interface', Doctrine\ORM\Version::VERSION); 
$cli->setCatchExceptions(true); 
$cli->setHelperSet($helperSet); 
$cli->addCommands(array(
    // DBAL Commands 
    new \Doctrine\DBAL\Tools\Console\Command\RunSqlCommand(), 
    new \Doctrine\DBAL\Tools\Console\Command\ImportCommand(), 
    // ORM Commands 
    new \Doctrine\ORM\Tools\Console\Command\ClearCache\MetadataCommand(), 
    new \Doctrine\ORM\Tools\Console\Command\ClearCache\ResultCommand(), 
    new \Doctrine\ORM\Tools\Console\Command\ClearCache\QueryCommand(), 
    new \Doctrine\ORM\Tools\Console\Command\SchemaTool\CreateCommand(), 
    new \Doctrine\ORM\Tools\Console\Command\SchemaTool\UpdateCommand(), 
    new \Doctrine\ORM\Tools\Console\Command\SchemaTool\DropCommand(), 
    new \Doctrine\ORM\Tools\Console\Command\EnsureProductionSettingsCommand(), 
    new \Doctrine\ORM\Tools\Console\Command\ConvertDoctrine1SchemaCommand(), 
    new \Doctrine\ORM\Tools\Console\Command\GenerateRepositoriesCommand(), 
    new \Doctrine\ORM\Tools\Console\Command\GenerateEntitiesCommand(), 
    new \Doctrine\ORM\Tools\Console\Command\GenerateProxiesCommand(), 
    new \Doctrine\ORM\Tools\Console\Command\ConvertMappingCommand(), 
    new \Doctrine\ORM\Tools\Console\Command\RunDqlCommand(), 
    new \Doctrine\ORM\Tools\Console\Command\ValidateSchemaCommand(), 
)); 

$cli->run(); 

ici est le fichier de mappage XML pour le produit Exemple:

<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping 
        http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd"> 

     <entity name="models\entities\Product" table="products"> 
      <id name="id" type="integer" column="product_id"> 
       <generator strategy="AUTO" /> 
      </id> 

      <field name="name" column="product_name" type="string" /> 
     </entity> 

</doctrine-mapping> 

Comme je l'ai dit, lorsque je tente de créer les entités tout fonctionne bien:

./doctrine orm:generate-entities ../application 
Processing entity "models\entities\Product" 

Entity classes generated to "/var/www/mysite/application" 

Et le product.php est généré dans les modèles Application \ \ entités répertoire \.

Mais lorsque je tente de générer le schéma que je reçois les exceptions/erreurs suivantes:

./doctrine orm:schema-tool:create 
PHP Warning: class_parents(): Class models\entities\Product does not exist and could not be loaded in /var/www/mysite/library/Doctrine/ORM/Mapping/ClassMetadataFactory.php on line 224 
PHP Warning: array_reverse() expects parameter 1 to be array, boolean given in /var/www/mysite/library/Doctrine/ORM/Mapping/ClassMetadataFactory.php on line 224 
PHP Warning: Invalid argument supplied for foreach() in /var/www/mysite/library/Doctrine/ORM/Mapping/ClassMetadataFactory.php on line 224 



    [ReflectionException] 
    Class models\entities\Product does not exist 



orm:schema-tool:create [--dump-sql] [-h|--help] [-q|--quiet] [-v|--verbose] [-V|--version] [-c|--color] [-n|--no-interaction] command 



Warning: class_parents(): Class models\entities\Product does not exist and could not be loaded in /var/www/mysite/library/Doctrine/ORM/Mapping/ClassMetadataFactory.php on line 224 

Warning: array_reverse() expects parameter 1 to be array, boolean given in /var/www/mysite/library/Doctrine/ORM/Mapping/ClassMetadataFactory.php on line 224 

Warning: Invalid argument supplied for foreach() in /var/www/mysite/library/Doctrine/ORM/Mapping/ClassMetadataFactory.php on line 224 

Merci pour votre aide.

+0

Je pense que cela pourrait aider: [http://stackoverflow.com/questions/4860856/doctrine-2-0-reflectionexception-when- i-try-to-do-yaml-mapping] [1] [1]: http://stackoverflow.com/questions/4860856/doctrine-2-0-reflectionexception-when-i-try -to-do-yaml-mapping –

Répondre

3

Je vois quelques problèmes avec ce code:

$classLoader = new \Doctrine\Common\ClassLoader('Entities', APPLICATION_PATH . '/models/entities'); 
$classLoader->setNamespaceSeparator('_'); 
  1. Dans vos fichiers de mappage, vous déclarez vos noms de classe d'entités en utilisant le séparateur backslash. Vous ne semblez pas utiliser de noms de classe hérités dans vos entités et n'avez pas besoin de changer le séparateur. Dans vos fichiers de mappage, vous déclarez que l'espace de nom complet de votre entité est models\entities\Product. Cependant, vous enregistrez uniquement l'espace de noms Entities dans votre fichier de configuration. Vous devez l'enregistrer en tant que Models pour que Doctrine effectue correctement la résolution de l'espace de noms. Je voudrais également associez pas le cas dans l'espace de noms (devrait être Models\Entities\Product).

  2. Enfin, lors de l'enregistrement namespaces, Doctrine commencera à la recherche pour les classes de la piste de base que vous fournissez et ajoutez l'espace de noms. Enregistrement Alors Entities que vous avez, Doctrine va chercher quoi que ce soit dans l'espace de noms Entities sous application/models/entities/Entities/

Si vous voulez que votre espace de noms pour les classes de modèle à Models\Entities\Product, utilisez cette option pour charger l'espace de noms (et renommer votre dossier models-Models):.

$classLoader = new \Doctrine\Common\ClassLoader('Models', APPLICATION_PATH); 

Typiquement, j'utilise un espace de noms au niveau de l'application pour tout mettre sous.Donc, mes modèles seraient App\Entities\Product et mon espace de nom autoload ressemble:

$classLoader = new \Doctrine\Common\ClassLoader('App', APPLICATION_PATH . '/models'); 
+0

En effet, le problème venait du cas de mélange dans l'espace de noms et en général de l'espace de noms. Merci beaucoup! – stormblow

Questions connexes