离Zend很长一段时间后,我需要再次回到它。 我在Zend Framework上的最后一项工作是在2016年。现在我尝试使用doctrine安装Zend Framework 3并且卡住了。
我做了什么:
使用composer构建骨架应用
composer create-project -s dev zendframework/skeleton-application path/to/install
没有使用最小安装,并在所有选项上说y。更新了作曲家并安装了Zend Framework:
composer self-update
composer install
我启动了机器并输入了它:vagrant up && vagrant ssh
我需要doctrine 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 Application\Controller\Factory;
use Application\Entity\User;
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);
}
}
在代码库中添加了一个实体:
namespace Application\Entity;
/**
* @ORM\Entity
* @ORM\Table(name="user")
*/
class User
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(name="id")
*/
protected $id;
/**
* @ORM\Column(name="nick")
*/
protected $nick;
....
}
我尝试使用此实体通过doctrine创建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 Doctrine\ORM\EntityManager
*/
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('Application\Entity\User')->findAll();
return new ViewModel();
}
}
导致以下错误:
An error occurred
An error occurred during execution; please try again later.
Additional information: Doctrine\Common\Persistence\Mapping\MappingException
File: /var/www/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/MappingException.php:37
Message: The class 'Application\Entity\User' 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): Doctrine\Common\Persistence\Mapping\MappingException::classNotFoundInNamespaces('Application\\Ent...', Array)
#1 /var/www/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php(151): Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain->loadMetadataForClass('Application\\Ent...', Object(Doctrine\ORM\Mapping\ClassMetadata))
#2 /var/www/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php(332): Doctrine\ORM\Mapping\ClassMetadataFactory->doLoadMetadata(Object(Doctrine\ORM\Mapping\ClassMetadata), NULL, false, Array)
#3 /var/www/vendor/doctrine/orm/lib/Doctrine/ORM/Mapping/ClassMetadataFactory.php(78): Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory->loadMetadata('Application\\Ent...')
#4 /var/www/vendor/doctrine/common/lib/Doctrine/Common/Persistence/Mapping/AbstractClassMetadataFactory.php(216): Doctrine\ORM\Mapping\ClassMetadataFactory->loadMetadata('Application\\Ent...')
#5 /var/www/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php(281): Doctrine\Common\Persistence\Mapping\AbstractClassMetadataFactory->getMetadataFor('Application\\Ent...')
#6 /var/www/vendor/doctrine/orm/lib/Doctrine/ORM/Repository/DefaultRepositoryFactory.php(44): Doctrine\ORM\EntityManager->getClassMetadata('Application\\Ent...')
#7 /var/www/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php(698): Doctrine\ORM\Repository\DefaultRepositoryFactory->getRepository(Object(Doctrine\ORM\EntityManager), 'Application\\Ent...')
#8 /var/www/module/Application/src/Controller/IndexController.php(35): Doctrine\ORM\EntityManager->getRepository('Application\\Ent...')
#9 /var/www/vendor/zendframework/zend-mvc/src/Controller/AbstractActionController.php(78): Application\Controller\IndexController->indexAction()
#10 /var/www/vendor/zendframework/zend-eventmanager/src/EventManager.php(322): Zend\Mvc\Controller\AbstractActionController->onDispatch(Object(Zend\Mvc\MvcEvent))
#11 /var/www/vendor/zendframework/zend-eventmanager/src/EventManager.php(179): Zend\EventManager\EventManager->triggerListeners(Object(Zend\Mvc\MvcEvent), Object(Closure))
#12 /var/www/vendor/zendframework/zend-mvc/src/Controller/AbstractController.php(106): Zend\EventManager\EventManager->triggerEventUntil(Object(Closure), Object(Zend\Mvc\MvcEvent))
#13 /var/www/vendor/zendframework/zend-mvc/src/DispatchListener.php(138): Zend\Mvc\Controller\AbstractController->dispatch(Object(Zend\Http\PhpEnvironment\Request), Object(Zend\Http\PhpEnvironment\Response))
#14 /var/www/vendor/zendframework/zend-eventmanager/src/EventManager.php(322): Zend\Mvc\DispatchListener->onDispatch(Object(Zend\Mvc\MvcEvent))
#15 /var/www/vendor/zendframework/zend-eventmanager/src/EventManager.php(179): Zend\EventManager\EventManager->triggerListeners(Object(Zend\Mvc\MvcEvent), Object(Closure))
#16 /var/www/vendor/zendframework/zend-mvc/src/Application.php(332): Zend\EventManager\EventManager->triggerEventUntil(Object(Closure), Object(Zend\Mvc\MvcEvent))
#17 /var/www/public/index.php(40): Zend\Mvc\Application->run()
#18 {main}
有人能给我一些提示吗?
答案 0 :(得分:0)
答案 1 :(得分:0)
乍一看,您的错误似乎告诉我们他找不到'Application \ Entity \ User'。 一开始,Doctrine对您的命名空间一无所知。 Doctrine使用AnnotationDriver了解实体的位置。 因此,在指定module.config.php的第5步中,您需要将以下代码添加到组合中:
^(?=.*\S)(?!.*\d).*
当然,你需要设置路径到你的设置。