Symfony 4未从现有数据库生成getter和setter

4

我知道在Symfony 4中从现有数据库生成实体,只需要执行以下命令:

> php bin/console doctrine:generate:entities

但是这个命令不会生成getter setter,只是从列中生成变量。

当然,我必须手动创建一些脏代码才能完成它。

也许我错过了文档,如何使用Doctrine Symfony 4从现有数据库生成实体的getter和setter?


可能是重复问题:https://dev59.com/EWEi5IYBdhLWcg3wZ7ka - Aurelien
3个回答

13

要从现有数据库生成实体类,您需要要求Doctrine自省数据库并生成相应的元数据文件。元数据文件根据表字段描述要生成的实体类。

> php bin/console doctrine:mapping:import App\\Entity annotation --path=src/Entity

这个命令将在src/Entity目录下生成带有注释元数据的新PHP类。 要生成缺失的getter/setter方法(或者在必要时创建类),请运行:

该命令将生成带有注释元数据的新PHP类到src/Entity。

要生成缺失的getter/setter方法(或创建必要的类),请执行:

> php bin/console make:entity --regenerate App

也可以查看官方文档


1

DOC says: "If you prefer to add new properties manually, the make:entity command can generate the getter & setter methods for you:

php bin/console make:entity --regenerate

If you make some changes and want to regenerate all getter/setter methods, also pass --overwrite."

请注意,对于Symfony 3(我不知道v4),doctrine:generate:entities无法使用protected属性。


0

如果要从现有数据库中进行操作,可以使用反向工程。按照以下步骤进行:

  1. 在.env文件中配置您的数据库

2.1 使用以下命令从已配置的数据库创建实体:

php bin/console doctrine:mapping:convert --from-database annotation ./src/Entity

2.2. 如果您想为特定的表创建实体:

php bin/console doctrine:mapping:convert --from-database --filter="Tablename" annotation ./src/Entity

如果你使用vim,你可以安装php-getter-setter.vim插件并在每个实体中输入以下内容来添加getter和setter: :% InsertBothGetterSetter 你必须手动在每个实体中添加: namespace App\Entity;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接