Laravel 中实体的逆向工程或自动生成?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20271670/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 08:42:51  来源:igfitidea点击:

Reverse engineering or Auto-Generations of Entities in Laravel?

phplaravelreverse-engineering

提问by Lory Huz

I usually work with SF2, and with Doctrine, Entities can be generated automatically and if you build the schema in a Soft like MySQL Workbench you can do Reverse Engineering.

我通常使用 SF2,并且使用 Doctrine,可以自动生成实体,如果您在像 MySQL Workbench 这样的 Soft 中构建架构,您可以进行逆向工程。

I'm new to Laravel so there is a way to do these both things? I would use Laravel because I've to do a very little project, but I didn't want to write all this code for what we call "Migrations", seems very boring no?

我是 Laravel 的新手,所以有办法做到这两件事吗?我会使用 Laravel 因为我要做一个很小的项目,但我不想为我们所谓的“迁移”编写所有这些代码,看起来很无聊不是吗?

So there is a way to generate this stuff in laravel? Maybe I've to use Doctrine in Laravel for that?

那么有没有办法在 Laravel 中生成这些东西?也许我必须在 Laravel 中使用 Doctrine?

回答by Gadoma

If you want to generate migration files from an existing mysql database you can use a Laravel package called XCMer / larry-four-generator. Apart from reverse engineering it has a bunch of other features.

如果您想从现有的 mysql 数据库生成迁移文件,您可以使用名为 XCMer / larry-four-generator 的 Laravel 包。除了逆向工程之外,它还具有许多其他功能。

Larry Four is a Laravel 4 package offering advanced model and migration generation functionality. Thanks to Larry you can quickly jot down the data scheme for your idea in an easy DSL and genereate migrations and models from it with just a single click. Larry can also analyse your existing data scheme and generate some magic for you too.

Larry Four 是 Laravel 4 软件包,提供高级模型和迁移生成功能。感谢 Larry,您可以在简单的 DSL 中快速记下您的想法的数据方案,只需单击一下即可从中生成迁移和模型。Larry 还可以分析您现有的数据方案并为您生成一些魔法。

Try the original best master branch
https://github.com/XCMer/larry-four-generator/tree/master

试试原来最好的master分支
https://github.com/XCMer/larry-four-generator/tree/master

or my Extended Models fork offering additional functionality
https://github.com/XCMer/larry-four-generator/tree/Gadoma-extendedmodels

或我的扩展模型分支提供附加功能
https://github.com/XCMer/larry-four-generator/tree/Gadoma-extendedmodels

回答by Antonio Carlos Ribeiro

As far as I can tell there is still no way to reverse a schema to migration files, using Laravel only. But you can:

据我所知,仅使用 Laravel 仍然无法将架构反转为迁移文件。但是你可以:

1) Export your MySQL schema

1) 导出您的 MySQL 架构

mysqldump -u root -p --no-data dbname > schema.sql

2) Create a migration to use your schema

2) 创建迁移以使用您的架构

php artisan migration:make create_schema

3) Boot your full schema

3) 启动您的完整架构

class CreateSchema extends Migration {

    public function up()
    {
        $file = file_get_contents(app_path().'/database/data/full_schema.sql', true);

        DB::unprepared($file);
    }

    public function down()
    {
    }

}

4) After that, if you need to do any changes to your schema, just create new migrations and make your changes.

4) 之后,如果您需要对架构进行任何更改,只需创建新的迁移并进行更改。

Not tested, but should work.

未经测试,但应该可以工作。