实体映射不起作用



我正在尝试将Doctrine2与ZF3一起使用。所有组件都已安装作曲家。

当我尝试使用实体时,我有一个例外:

Doctrine\Common\Persistence\Mapping\MappingException

类"应用程序\实体\连接"不存在

当我在控制器的操作中使用时,会引发异常:

$concours = $this->entityManager->getRepository(Concours::class);

如果我使用:

$entity = new Concours();

$entity = new ApplicationEntityConcours();

我真的不明白为什么...

感谢您的帮助...

我的配置:

配置/本地.php

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

config/modules.config.php

return [
'ZendCache',
'ZendForm',
'ZendInputFilter',
'ZendFilter',
'ZendPaginator',
'ZendHydrator',
'ZendRouter',
'ZendValidator',
'DoctrineModule',
'DoctrineORMModule',
'Application',
];

对于应用模块,

module/Application/config/module.config.php

namespace Application;
use ZendRouterHttpLiteral;
use ZendRouterHttpSegment;
use ZendServiceManagerFactoryInvokableFactory;
use DoctrineORMMappingDriverAnnotationDriver;

return [
'router' => [
'routes' => [
'home' => [
'type' => Literal::class,
'options' => [
'route'    => '/',
'defaults' => [
'controller' => ControllerIndexController::class,
'action'     => 'index',
],
],
],
'application' => [
'type'    => Segment::class,
'options' => [
'route'    => '/application[/:action]',
'defaults' => [
'controller' => ControllerIndexController::class,
'action'     => 'index',
],
],
],
],
],
'controllers' => [
'factories' => [
ControllerIndexController::class =>  ControllerFactoryIndexControllerFactory::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'
]
]
]
]  
];

我的控制器(模块/应用程序/src/控制器/索引控制器.php)是:

namespace ApplicationController;
use ZendMvcControllerAbstractActionController;
use ZendViewModelViewModel;
use DoctrineORMEntityManager;
use ApplicationEntityConcours;
class IndexController extends AbstractActionController {
/**
* Entity manager.
* @var DoctrineORMEntityManager
*/
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 ApplicationControllerFactory;
use InteropContainerContainerInterface;
use ZendServiceManagerFactoryFactoryInterface;
use ApplicationControllerIndexController;
/**
* 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/应用程序/实体/中, 下面是 Concours 实体类:

<?php
namespace ApplicationEntity;
use DoctrineORMMapping as ORM;
/**
* Concours
*
* @ORMTable(name="Concours", uniqueConstraints={@ORMUniqueConstraint(name="numero_UNIQUE", columns={"numero"})})
* @ORMEntity
*/
class Concours
{
/**
* @var integer
*
* @ORMColumn(name="ref", type="integer", nullable=false)
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
*/
private $ref;
...
}

这是你的问题:

我所有的实体类文件都在 模块/应用程序/src/应用程序/实体/

Zend 3 使用 PSR-4 而不是 PSR-0,后者被认为是已弃用的。您可以通过检查composer.json文件来确认它。您应该看到以下声明:

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

PSR-0中,如果您定义命名空间FooBarsrc/它将查找src/Foo/Bar/{your_class}.php中的类,而在PSR-4中,它将查找src/{your_class}.php.

所以...来解决你的问题。Concours实体从以下位置移动:

模块/应用程序/

src/应用程序/实体/

自:

模块/应用程序/src/实体/

相关内容

  • 没有找到相关文章

最新更新