如何从数据库生成主义实体并使用PSR-4自动加载? [英] How to generate Doctrine entities From Database and Use PSR-4 Autoloading?
问题描述
在PSR-4自动加载中使用Doctrine 2.5
并将已设计的数据库模式转换为实体类(注释).问题是要以正确的目录结构获取导出的文件.
Using Doctrine 2.5
with PSR-4 autoloading and converting an already designed database schema to entity classes (annotations). The problem is getting the exported files in the correct directory structure.
composer.json
{
"autoload": {
"psr-4": {
"Application\\": "src/"
}
},
"require": {
"doctrine/orm": "^2.5"
}
}
orm:convert-mapping
vendor/bin/doctrine orm:convert-mapping \
--namespace='Application\Entity\' \
--force \
--from-database \
annotation \
src/
运行此命令将在src/
中添加一个Application
目录.
生成的类文件具有正确的名称空间,但在PSR-4标准的错误目录中.
Running this command will add an Application
directory in src/
.
The generated class file has the correct namespace but in the wrong directory for PSR-4 standard.
<?php
namespace Application\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* User
*
* @ORM\Table(name="user")
* @ORM\Entity
*/
class User
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", nullable=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
}
有没有一种方法可以解决此问题而无需辅助命令?
Is there a way to solve this without a secondary command?
推荐答案
我看不到doctrine cli
的任何选项.我只是看到了从应用程序模块修改目录结构的解决方案.在这里,我修改composer.json
I don't see any option from doctrine cli
for this needed. I just see the solution to modify the directory structure from your Application module. Here I modify the composer.json
{
"autoload": {
"psr-4": {
"Application\\": "src/Application/"
}
},
"require": {
"doctrine/orm": "^2.5"
}
}
所有Application
模块源代码都将放置在src/Application
而不是src/
上.因此,当doctrine cli
在src
中创建目录Application/Entity
时,它将与yor psr-4
Autoloader匹配.
All Application
module source code will be put on src/Application
not src/
anymore. So, when doctrine cli
create directory Application/Entity
in src
, it will match with yor psr-4
Autoloader.
这篇关于如何从数据库生成主义实体并使用PSR-4自动加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!