简体   繁体   中英

Doctrine 2.0: get classloader to work for entities

I've been struggling quite some time to get my entities loaded in my first Doctrine 2.0 project. Everything works fine (got the other classes to load, connection with database trough DBAL is successful) except for loading my entity classes.

I'll give you the information you need.

  • I installed Doctrine trough the tarball method
  • the structure of my folder is like this

     public_html -> docrine test -> entities -> User.php -> Video.php 
  • in my bootstrap file I'm trying to load it with

     <?php $sRoot = "/home/..../public_html/doctrinetest"; $classLoader = new \\Doctrine\\Common\\ClassLoader('doctrinetest\\entities', $sRoot.'/doctrinetest/entities'); $classLoader->register(); // register on SPL autoload stack 
  • As namespace, I put the following line before defying the class

     namespace doctrinetest\\entities; 
  • When I then try to run the command to generate my scheme

     $tool = new \\Doctrine\\ORM\\Tools\\SchemaTool($em); $classes = array( $em->getClassMetadata('Video'), $em->getClassMetadata('User') ); $tool->createSchema($classes); 
  • I get the error

     Warning: class_parents() [function.class-parents]: Class Video does not exist and could not be loaded in /home/..../public_html/doctrine2-tarball/Doctrine/Common/Persistence/Mapping/RuntimeReflectionService.php on line 40 

Please help me on this one Thanks, Pj

First, \\Doctrine\\Common\\ClassLoader find classes in 'includePath/namespace/' directory.

<?php
$sRoot = "/home/..../public_html/doctrinetest";
$classLoader = new \Doctrine\Common\ClassLoader('doctrinetest\entities', $sRoot.'/doctrinetest/entities');
$classLoader->register();
?>

The above code try to find class in '/home/..../public_html/doctrinetest/doctrinetest/entities/doctrinetest/entities/' directory. Of course, cannot found classes.

Second, getClassMetadata function of EntityManager want className including namespace for an argument.

Therefore use like this.

$em->getClassMetadata('\\doctrinetest\\entities\\Video');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM