2013-02-11 129 views
0

使用zend 2与标准的应用程序结构。 我有实体:doctrine 2 setHydrator错误

module\University\src\University\Entity\Student.php 
<?php 
namespace University\Entity; 

use Doctrine\Common\Collections\ArrayCollection; 
use Doctrine\Common\Collections\Collection; 
use Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Entity 
* @ORM\Table(name="students") 
*/ 
class Student 
{ //more stuff 
} 

和形式

University\src\University\Form\StudentForm.php 
<?php 
namespace University\Form; 

use DoctrineModule\Stdlib\Hydrator\DoctrineObject as DoctrineHydrator; 
use Zend\Form\Form; 
use Zend\ServiceManager\ServiceManager; 

class StudentForm extends Form 
{ 
    parent::__construct('student-form'); 
    $entityManager = $serviceManager->get('Doctrine\ORM\EntityManager'); 

    $this->setHydrator(new DoctrineHydrator($entityManager, 'University\Entity\Student')); // this row cause the error 

    // more stuff 
} 

在控制器:

<?php 
namespace University\Controller; 

use Zend\Mvc\Controller\AbstractActionController, 
    Zend\View\Model\ViewModel, 
    University\Entity\Student, 
    University\Form\StudentForm; 

class StudentController extends AbstractActionController 
{ 
    public function indexAction() 
    { 

     $form = new StudentForm($this->serviceLocator); 
     // more stuffs 
    } 
} 

和学说抛出我的错误:

File: 
\vendor\doctrine\common\lib\Doctrine\Common\Persistence\Mapping\MappingException.php:38 
Message: 
The class 'University\Entity\Student' was not found in the chain configured namespaces \Entity 

IM以下this教程。

任何想法,为什么我得到这个错误,以及如何解决它?

编辑 我发现如何解决这个问题,以及为什么造成这个问题。答案转移到主题答案中。'

+0

第一次猜测:原理错误的驱动程序配置。由于错误,我的绝对猜测是''namespace University;'在doctrine-config中缺少。 – Sam 2013-02-11 19:41:11

+0

确实。 @Tudor请粘贴相关配置(https://github.com/doctrine/DoctrineORMModule自述文件中的一个) – Ocramius 2013-02-11 21:09:21

+0

嗨,大家好,感谢您的快速响应,并对我的慢速回复感到抱歉:> 您说得对,我懂了现在工作,但用这个配置在module.config.php(即时编辑主帖) – Todor 2013-02-12 08:36:42

回答

0

我找到了解决方案。这个问题在我的module.config.php,我用的是以下原则设置:

'doctrine' => array(
    'driver' => array(
     __NAMESPACE__ . '_driver' => array(
      'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver', 
      'cache' => 'array', 
      'paths' => array(__DIR__ . '/../src/' . __NAMESPACE__ . '/Entity') 
     ), 
     'orm_default' => array(
      'drivers' => array(
       __NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver' 
      ) 
     ) 
    ) 
) 

不是很确定如何修改上面的代码得到它的工作, 但这是我如何做它的工作:

// Doctrine config 
'doctrine' => array(
    'driver' => array(
     // defines an annotation driver with two paths, and names it `my_annotation_driver` 
     'my_annotation_driver' => array(
      'class' => 'Doctrine\ORM\Mapping\Driver\AnnotationDriver', 
      'cache' => 'array', 
      'paths' => array(
       __DIR__ . '/../src/University/Entity', 
       'another/path' 
      ), 
     ), 

     // default metadata driver, aggregates all other drivers into a single one. 
     // Override `orm_default` only if you know what you're doing 
     'orm_default' => array(
      'drivers' => array(
       // register `my_annotation_driver` for any entity under namespace `My\Namespace` 
       'University\Entity' => 'my_annotation_driver' 
      ) 
     ) 
    ) 
) 

10x u guys for the help!

编辑: 我找出为什么第一次的配置(与NAMESPACES)没有工作,其因有wasnt设置module.config.php文件的顶部任何命名空间,所以如果u希望配置与NAMESPACE批注,只需添加'命名空间YourModuleName;'