在 Zend3 项目中集成 Doctrine 的问题



离开Zend很久后,我需要再次回到它。 我上一次在Zend Framework上的工作是在2016年。现在我试图用教义安装 Zend Framework 3 并陷入困境。

我做了什么:

使用作曲家构建骨架应用

composer create-project -s dev zendframework/skeleton-application path/to/install

没有使用最小安装,并在所有选项上说y。更新了作曲家并安装了Zend框架:

composer self-update
composer install

我启动机器并输入:vagrant up && vagrant ssh

我需要原则require doctrine/doctrine-orm-module并在 module.config 中对其进行配置.php如下所示:

'doctrine' => [
'connection' => [
'orm_default' => [
'driverClass' => PDOMySqlDriver::class,
'params' => [
'host'     => '127.0.0.1',
'user'     => 'root',
'password' => 'r00t',
'dbname'   => 'zftest',
]
],
],
],

然后我写了一个控制器工厂来注入实体管理器:

namespace ApplicationControllerFactory;
use ApplicationEntityUser;
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);
}
}

将实体添加到代码库:

namespace ApplicationEntity;
/**
* @ORMEntity
* @ORMTable(name="user")
*/
class User
{
/**
* @ORMId
* @ORMGeneratedValue
* @ORMColumn(name="id")
*/
protected $id;
/**
* @ORMColumn(name="nick")
*/
protected $nick; 
....
}

我尝试使用此实体通过教义创建 mysql 表:

./vendor/bin/doctrine-module orm:schema-tool:create

表未创建,我收到此错误:

"没有要处理的元数据类。">

我试图测试教义:

./vendor/bin/doctrine-module orm:validate-schema

它似乎正在工作:

[Mapping]  OK - The mapping files are correct.
[Database] OK - The database schema is in sync with the mapping files.

还尝试在控制器中加载我手动创建的表:

class IndexController extends AbstractActionController
{
/**
* Doctrine entity manager.
* @var DoctrineORMEntityManager
*/
private $entityManager;
// Constructor is used to inject dependencies into the service.
/**
* IndexController constructor.
* @param $entityManager
*/
public function __construct($entityManager)
{
$this->entityManager = $entityManager;
}
public function indexAction()
{
$users = $this->entityManager->getRepository('ApplicationEntityUser')->findAll();
return new ViewModel();
}
}

这导致了以下错误:

An error occurred
An error occurred during execution; please try again later.
Additional information: DoctrineCommonPersistenceMappingMappingException
File: /var/www/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php:37
Message: The class 'ApplicationEntityUser' was not found in the chain configured namespaces 
Stack trace:
#0 /var/www/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/Driver/MappingDriverChain.php(112): DoctrineCommonPersistenceMappingMappingException::classNotFoundInNamespaces('Application\Ent...', Array)
#1 /var/www/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php(151): DoctrineCommonPersistenceMappingDriverMappingDriverChain->loadMetadataForClass('Application\Ent...', Object(DoctrineORMMappingClassMetadata))
#2 /var/www/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php(332): DoctrineORMMappingClassMetadataFactory->doLoadMetadata(Object(DoctrineORMMappingClassMetadata), NULL, false, Array)
#3 /var/www/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php(78): DoctrineCommonPersistenceMappingAbstractClassMetadataFactory->loadMetadata('Application\Ent...')
#4 /var/www/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php(216): DoctrineORMMappingClassMetadataFactory->loadMetadata('Application\Ent...')
#5 /var/www/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php(281): DoctrineCommonPersistenceMappingAbstractClassMetadataFactory->getMetadataFor('Application\Ent...')
#6 /var/www/vendor/doctrine/orm/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php(44): DoctrineORMEntityManager->getClassMetadata('Application\Ent...')
#7 /var/www/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php(698): DoctrineORMRepositoryDefaultRepositoryFactory->getRepository(Object(DoctrineORMEntityManager), 'Application\Ent...')
#8 /var/www/module/Application/src/Controller/IndexController.php(35): DoctrineORMEntityManager->getRepository('Application\Ent...')
#9 /var/www/vendor/zendframework/zend-mvc/src/Controller/AbstractActionController.php(78): ApplicationControllerIndexController->indexAction()
#10 /var/www/vendor/zendframework/zend-eventmanager/src/EventManager.php(322): ZendMvcControllerAbstractActionController->onDispatch(Object(ZendMvcMvcEvent))
#11 /var/www/vendor/zendframework/zend-eventmanager/src/EventManager.php(179): ZendEventManagerEventManager->triggerListeners(Object(ZendMvcMvcEvent), Object(Closure))
#12 /var/www/vendor/zendframework/zend-mvc/src/Controller/AbstractController.php(106): ZendEventManagerEventManager->triggerEventUntil(Object(Closure), Object(ZendMvcMvcEvent))
#13 /var/www/vendor/zendframework/zend-mvc/src/DispatchListener.php(138): ZendMvcControllerAbstractController->dispatch(Object(ZendHttpPhpEnvironmentRequest), Object(ZendHttpPhpEnvironmentResponse))
#14 /var/www/vendor/zendframework/zend-eventmanager/src/EventManager.php(322): ZendMvcDispatchListener->onDispatch(Object(ZendMvcMvcEvent))
#15 /var/www/vendor/zendframework/zend-eventmanager/src/EventManager.php(179): ZendEventManagerEventManager->triggerListeners(Object(ZendMvcMvcEvent), Object(Closure))
#16 /var/www/vendor/zendframework/zend-mvc/src/Application.php(332): ZendEventManagerEventManager->triggerEventUntil(Object(Closure), Object(ZendMvcMvcEvent))
#17 /var/www/public/index.php(40): ZendMvcApplication->run()
#18 {main}

有人可以给我一个提示,我错过了什么吗?

看起来您没有定义应该在哪里找到实体。

下面是代码中缺失位的链接:

教义ORMModule 文档

乍一看,您的错误似乎告诉我们他找不到"应用程序\实体\用户"。 一开始,Doctrine 对你的命名空间一无所知。 Doctrine 使用注释驱动程序来获取有关实体位置的通知。 因此,在步骤 5(指定 module.config.php)中,您需要将以下代码添加到组合中:

<?php
namespace Application;
use DoctrineORMMappingDriverAnnotationDriver;
return [
// ...
'doctrine' => [
'driver' => [
__NAMESPACE__ . '_driver' => [
'class' => AnnotationDriver::class,
'cache' => 'array',
'paths' => [__DIR__ . '/../src/Entity']
],
'orm_default' => [
'drivers' => [
__NAMESPACE__ . 'Entity' => __NAMESPACE__ . '_driver'
]
]
]
]  
];

当然,您需要设置到设置的路径。

相关内容

  • 没有找到相关文章

最新更新