2017-03-23 89 views
0

我正在嘗試使用ZF3的Doctrine2。所有組件都與作曲家一起安裝。實體映射不起作用

當我嘗試使用實體我有一個例外:

主義\ COMMON \持久性\製圖\ MappingException

級 '應用程序\實體\競賽' 不存在

$concours = $this->entityManager->getRepository(Concours::class); 
:當我在控制器的操作使用

拋出異常

我有同樣的問題,如果我使用:

$entity = new Concours(); 

$entity = new \Application\Entity\Concours(); 

我真的不明白爲什麼...

感謝您的幫助......

我的配置:

config/local.php

use Doctrine\DBAL\Driver\PDOMySql\Driver as PDOMySqlDriver; 
return [ 
    'doctrine' => [ 
     'connection' => [ 
      'orm_default' => [ 
       'driverClass' => PDOMySqlDriver::class, 
       'params' => [ 
        'host'  => '127.0.0.1',      
        'user'  => 'xxxx', 
        'password' => 'xxxx', 
        'dbname' => 'goch', 
       ], 
      ],    
     ],   
    ], 
]; 

配置/ modules.config.php

return [ 
    'Zend\Cache', 
    'Zend\Form', 
    'Zend\InputFilter', 
    'Zend\Filter', 
    'Zend\Paginator', 
    'Zend\Hydrator', 
    'Zend\Router', 
    'Zend\Validator', 
    'DoctrineModule', 
    'DoctrineORMModule', 
    'Application', 
]; 

對於應用模塊,

模塊/應用/配置/ module.config.php

namespace Application; 

use Zend\Router\Http\Literal; 
use Zend\Router\Http\Segment; 
use Zend\ServiceManager\Factory\InvokableFactory; 
use Doctrine\ORM\Mapping\Driver\AnnotationDriver; 


return [ 
    'router' => [ 
     'routes' => [ 
      'home' => [ 
       'type' => Literal::class, 
       'options' => [ 
        'route' => '/', 
        'defaults' => [ 
         'controller' => Controller\IndexController::class, 
         'action'  => 'index', 
        ], 
       ], 
      ], 
      'application' => [ 
       'type' => Segment::class, 
       'options' => [ 
        'route' => '/application[/:action]', 
        'defaults' => [ 
         'controller' => Controller\IndexController::class, 
         'action'  => 'index', 
        ], 
       ], 
      ], 
     ], 
    ], 
    'controllers' => [ 
     'factories' => [ 
      Controller\IndexController::class => Controller\Factory\IndexControllerFactory::class, 
     ], 
    ], 
    'view_manager' => [ 
     'display_not_found_reason' => true, 
     'display_exceptions'  => true, 
     'doctype'     => 'HTML5', 
     'not_found_template'  => 'error/404', 
     'exception_template'  => 'error/index', 
     'template_map' => [ 
      'layout/layout'   => __DIR__ . '/../view/layout/layout.phtml', 
      'application/index/index' => __DIR__ . '/../view/application/index/index.phtml', 
      'error/404'    => __DIR__ . '/../view/error/404.phtml', 
      'error/index'    => __DIR__ . '/../view/error/index.phtml', 
     ], 
     'template_path_stack' => [ 
      __DIR__ . '/../view', 
     ], 
    ], 

    'doctrine' => [ 
     'driver' => [ 
      __NAMESPACE__ . '_driver' => [ 
       'class' => AnnotationDriver::class, 
       'cache' => 'array', 
       'paths' => [__DIR__ . '/../src/Entity'] 
      ], 
      'orm_default' => [ 
       'drivers' => [ 
        __NAMESPACE__ . '\Entity' => __NAMESPACE__ . '_driver' 
       ] 
      ] 
     ] 
    ] 
]; 

我的Cont輥(模塊/應用/ src目錄/控制器/ IndexController.php)是:

namespace Application\Controller; 

use Zend\Mvc\Controller\AbstractActionController; 
use Zend\View\Model\ViewModel; 
use Doctrine\ORM\EntityManager; 
use Application\Entity\Concours; 

class IndexController extends AbstractActionController { 

    /** 
    * Entity manager. 
    * @var Doctrine\ORM\EntityManager 
    */ 
    private $entityManager; 

    // Constructor method is used to inject dependencies to the controller. 
    public function __construct($entityManager) 
    { 
    $this->entityManager = $entityManager; 
    } 


    public function indexAction() { 
     return new ViewModel(); 
    } 


    public function concoursAction(){ 
     $concours=array(); 

$concours = $this->entityManager->getRepository(Concours::class); 
    // Render the view template 
    return new ViewModel([ 
     'concours' => $concours 
    ]);   

    } 
} 

和相關工廠

namespace Application\Controller\Factory; 

use Interop\Container\ContainerInterface; 
use Zend\ServiceManager\Factory\FactoryInterface; 
use Application\Controller\IndexController; 

/** 
* This is the factory for IndexController. Its purpose is to instantiate the 
* controller. 
*/ 
class IndexControllerFactory implements FactoryInterface 
{ 
    public function __invoke(ContainerInterface $container, 
        $requestedName, array $options = null) 
    { 
     $entityManager = $container->get('doctrine.entitymanager.orm_default'); 

     // Instantiate the controller and inject dependencies 
     return new IndexController($entityManager); 
    } 
} 

我所有的實體類文件中模塊/應用/ src目錄/應用程序/實體/, 這裏是競賽實體類:

<?php 

namespace Application\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Concours 
* 
* @ORM\Table(name="Concours", uniqueConstraints={@ORM\UniqueConstraint(name="numero_UNIQUE", columns={"numero"})}) 
* @ORM\Entity 
*/ 
class Concours 
{ 
    /** 
    * @var integer 
    * 
    * @ORM\Column(name="ref", type="integer", nullable=false) 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="IDENTITY") 
    */ 
    private $ref; 
... 
} 

回答

2

這是你的問題:

我所有的實體類文件中 模塊/應用/ src目錄/應用/實體/

的Zend 3使用PSR-4而不是作爲depracated這被認爲是PSR-0。您可以通過檢查composer.json文件來確認它。您應該看到如下聲明:

"autoload": { 
    "psr-4": { 
     "Application\\": "module/Application/src/", 
    } 
}, 

PSR-0如果你定義Foo\Bar命名空間中src/ achored它會尋找類src/Foo/Bar/{your_class}.phpPSR-4它將在src/{your_class}.php

所以...解決你的問題。從移動Concours實體:

模塊/應用/ src目錄/應用/實體/

到:

模塊/應用/ src目錄/實體/

+0

是的! !它解決了我的問題。 Thnak你好! –