当我想创建一个新实体时,我使用以下命令:
php 应用程序/控制台原则:生成实体
但这只会帮助我用它们的 setter 和 getter 生成属性
有没有一种方法可以帮助我生成实体之间的关系(例如 OneToMany 和 ManyToOne)?
当我想创建一个新实体时,我使用以下命令:
php 应用程序/控制台原则:生成实体
但这只会帮助我用它们的 setter 和 getter 生成属性
有没有一种方法可以帮助我生成实体之间的关系(例如 OneToMany 和 ManyToOne)?
你可以试试 ORM Designer。它并不便宜,但很有帮助:
5年后我正在回答我自己的问题。我这样做是因为我刚刚使用了 symfony4 的工具来生成实体,而且它非常棒。社区制作了如此出色的工具。
在 symfony4 中,您只需要执行命令,bin/console make:entity
然后按说选择您的字段名称user
,然后选择类型relation
,向导将处理该过程并为您生成所有需要的代码!
一个让我惊讶的例子:
New property name (press <return> to stop adding fields):
> user
Field type (enter ? to see all types) [string]:
> relation
What class should this entity be related to?:
> User
What type of relationship is this?
------------ ----------------------------------------------------------------
Type Description
------------ ----------------------------------------------------------------
ManyToOne Each Statement relates to (has) one User.
Each User can relate/has to (have) many Statement objects
OneToMany Each Statement relates can relate to (have) many User objects.
Each User relates to (has) one Statement
ManyToMany Each Statement relates can relate to (have) many User objects.
Each User can also relate to (have) many Statement objects
OneToOne Each Statement relates to (has) exactly one User.
Each User also relates to (has) exactly one Statement.
------------ ----------------------------------------------------------------
Relation type? [ManyToOne, OneToMany, ManyToMany, OneToOne]:
> ManyToOne
Is the Statement.user property allowed to be null (nullable)? (yes/no) [yes]:
> yes
Do you want to add a new property to User so that you can access/update Statement objects from it - e.g. $user->getStatements()? (yes/no) [yes]:
> yes
A new property will also be added to the User class so that you can access the related Statement objects from it.
New field name inside User [statements]:
>
updated: src/Entity/Statement.php
updated: src/Entity/User.php
Add another property? Enter the property name (or press <return> to stop adding fields):
>
Success!
Next: When you're ready, create a migration with make:migration