How to generate entities from database schema using doctrine-orm-module

1、安装好doctrine,在composer.json中添加如下

    "require": {
        "php": "^5.6 || ^7.0",
        "doctrine/doctrine-orm-module": "*",
    },

执行composer install

2、在yourAPP/config/modules.config.php添加doctrine module

复制代码
return [
    'Zend\ServiceManager\Di',
    'Zend\Session',
    'Zend\Mvc\Plugin\Prg',
    'Zend\Mvc\Plugin\Identity',
    'Zend\Mvc\Plugin\FlashMessenger',
    'Zend\Mvc\Plugin\FilePrg',
    'Zend\Mvc\I18n',
    'Zend\Mvc\Console',
    'Zend\Log',
    'Zend\Form',
    'Zend\Db',
    'Zend\Cache',
    'Zend\Router',
    'Zend\Validator',
    'ZendDeveloperTools',
    'Application',
    'DoctrineModule',
    'DoctrineORMModule',
];
复制代码

3、在yourAPP/config/autoload/local.php添加如下

复制代码
<?php
/**
 * Local Configuration Override
 *
 * This configuration override file is for overriding environment-specific and
 * security-sensitive configuration information. Copy this file without the
 * .dist extension at the end and populate values as needed.
 *
 * @NOTE: This file is ignored from Git by default with the .gitignore included
 * in ZendSkeletonApplication. This is a good practice, as it prevents sensitive
 * credentials from accidentally being committed into version control.
 */
use Doctrine\DBAL\Driver\PDOMySql\Driver as PDOMySqlDriver;
return [
    'doctrine' => [
        'connection' => [
            'orm_default' => [
                'driverClass' => PDOMySqlDriver::class,
                'params' => [
                    'host'     => '10.11.1.2',
                    'port'     => '3306',
                    'user'     => 'xxx',
                    'password' => 'xxx',
                    'dbname'   => 'user_shanmaohuwai',
                ]
            ],            
        ],        
    ],
];
复制代码

4、使用doctrine命令生成entity(https://www.e-learn.cn/content/wangluowenzhang/609311)

 

问题:

I am using "doctrine/doctrine-orm-module": "0.7.0" with ZF2.

Once I create Entities I usually run following commands to sync and generate database automatically according to my entities.

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

Is there a way to make this process reverse? I mean, Can I generate entities from existing database in mysql?

回答1:

We use a batch script:

@ECHO OFF

mkdir EXPORT
call .\vendor\bin\doctrine-module orm:convert-mapping --force --from-database annotation ./EXPORT/
call .\vendor\bin\doctrine-module orm:generate-entities ./EXPORT/ --generate-annotations=true

pause 

orm:convert-mapping and orm:generate-entities is probably what you are looking for.



回答2:

There's a nice blog written on this here

Edit: It can be done by using the commands below:
1. convert-mapping (Table & Entity):

./vendor/doctrine/doctrine-module/bin/doctrine-module orm:convert-mapping --namespace="Album\\Entity\\" --force  --from-database annotation ./module/Album/src/

2. Generates getter and setter

./vendor/doctrine/doctrine-module/bin/doctrine-module orm:generate-entities ./module/Album/src/ --generate-annotations=true

 

回答3:

Try just it

doctrine orm:convert-mapping -f --from-database annotation entities/

doctrine orm:generate-entities --generate-annotations="true" entities/

http://wildlyinaccurate.com/useful-doctrine-2-console-commands/

 

 

cd appdir
./vendor/doctrine/doctrine-module/bin/doctrine-module orm:convert-mapping --namespace="Application\\Entity\\" --force --from-database annotation ./module/Application/src/
./vendor/doctrine/doctrine-module/bin/doctrine-module orm:generate-entities ./module/Application/src/ --generate-annotations=true

 

 

posted on   yipianchuyun  阅读(408)  评论(0编辑  收藏  举报

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示