简体   繁体   English

Doctrine 2.0:使类加载器适用于实体

[英]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. 我一直在努力将实体加载到我的第一个Doctrine 2.0项目中。 Everything works fine (got the other classes to load, connection with database trough DBAL is successful) except for loading my entity classes. 除了加载我的实体类之外,其他所有东西都正常运行(让其他类加载,与数据库通过DBAL的连接成功)。

I'll give you the information you need. 我会为您提供所需的信息。

  • I installed Doctrine trough the tarball method 我安装了Toctrine槽的tarball方法
  • 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. 首先,\\ Doctrine \\ Common \\ ClassLoader在“ includePath / namespace /”目录中找到类。

<?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. 上面的代码尝试在“ /home/..../public_html/doctrinetest/doctrinetest/entities/doctrinetest/entities/”目录中找到类。 Of course, cannot found classes. 当然,找不到类。

Second, getClassMetadata function of EntityManager want className including namespace for an argument. 其次,EntityManager的getClassMetadata函数希望className 包括参数的名称空间

Therefore use like this. 因此像这样使用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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