Doctrine orm 实体映射不';行不通
我正在尝试将Doctrine2与ZF3一起使用。所有组件都已使用composer安装 当我尝试使用实体时,我有一个例外: 条令\Common\Persistence\Mapping\MappingException 类“Application\Entity\Concours”不存在 在控制器的操作中使用时引发异常:Doctrine orm 实体映射不';行不通,doctrine-orm,mapping,entity,zend-framework3,Doctrine Orm,Mapping,Entity,Zend Framework3,我正在尝试将Doctrine2与ZF3一起使用。所有组件都已使用composer安装 当我尝试使用实体时,我有一个例外: 条令\Common\Persistence\Mapping\MappingException 类“Application\Entity\Concours”不存在 在控制器的操作中使用时引发异常: $concours = $this->entityManager->getRepository(Concours::class); 如果我使用: $entity = n
$concours = $this->entityManager->getRepository(Concours::class);
如果我使用:
$entity = new 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',
],
],
],
],
];
config/modules.config.php
return [
'Zend\Cache',
'Zend\Form',
'Zend\InputFilter',
'Zend\Filter',
'Zend\Paginator',
'Zend\Hydrator',
'Zend\Router',
'Zend\Validator',
'DoctrineModule',
'DoctrineORMModule',
'Application',
];
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'
]
]
]
]
];
对于应用程序模块
module/Application/config/module.config.php
return [
'Zend\Cache',
'Zend\Form',
'Zend\InputFilter',
'Zend\Filter',
'Zend\Paginator',
'Zend\Hydrator',
'Zend\Router',
'Zend\Validator',
'DoctrineModule',
'DoctrineORMModule',
'Application',
];
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'
]
]
]
]
];
我的控制器(module/Application/src/Controller/IndexController.php)是:
以及相关工厂
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);
}
}
我的所有实体类文件都位于模块/Application/src/Application/Entity/中,
以下是Concours实体类:
<?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;
...
}
这是您的问题:
我所有的实体类文件都在
模块/应用程序/src/应用程序/实体/
Zend 3使用,而不是被视为已失种族的。您可以通过检查composer.json
文件来确认它。您应该看到以下声明:
"autoload": {
"psr-4": {
"Application\\": "module/Application/src/",
}
},
在PSR-0中,如果定义Foo\Bar
名称空间存储在src/
中,它将在src/Foo/Bar/{your_class}.php
中查找类,而在PSR-4中,它将在src/{your_class}.php
中查找类
所以。。。来解决你的问题。从以下位置移动concurs
实体:
模块/应用程序/src/应用程序/实体/
致:
模块/应用程序/src/实体/
这是你的问题:
我所有的实体类文件都在
模块/应用程序/src/应用程序/实体/
Zend 3使用,而不是被视为已失种族的。您可以通过检查composer.json
文件来确认它。您应该看到以下声明:
"autoload": {
"psr-4": {
"Application\\": "module/Application/src/",
}
},
在PSR-0中,如果定义Foo\Bar
名称空间存储在src/
中,它将在src/Foo/Bar/{your_class}.php
中查找类,而在PSR-4中,它将在src/{your_class}.php
中查找类
所以。。。来解决你的问题。从以下位置移动concurs
实体:
模块/应用程序/src/应用程序/实体/
致:
模块/应用程序/src/实体/