[英]Symfony2 + Doctrine : Tool for generating relations between entities
when I want to create a new entity I use the following command :当我想创建一个新实体时,我使用以下命令:
php app/console doctrine:generate entity php 应用程序/控制台原则:生成实体
But this will help me only to generate attributes with their setters and getters但这只会帮助我使用它们的 setter 和 getter 生成属性
Is there a way that helps me to generate also relations between entities ( OneToMany , and ManyToOne for example ) ?有没有一种方法可以帮助我生成实体之间的关系(例如 OneToMany 和 ManyToOne)?
You could try ORM Designer.你可以试试 ORM 设计器。 It's not cheap, but helpful:它不便宜,但很有帮助:
I am answering my own question after 5 years. 5年后我正在回答我自己的问题。 I am doing it because I juste used symfony4's tool for generating entities and It's super awesome.我这样做是因为我刚刚使用了 symfony4 的工具来生成实体,而且它非常棒。 A big up to the community for making such great tools.社区可以制作如此出色的工具。
In symfony4 you juste have to execute the commande bin/console make:entity
then choose your field name user
per say, then for the type choose relation
and a wizard will take trought the process and generates all the needed code for you !在 symfony4 中,您必须执行命令bin/console make:entity
然后选择您的字段名称user
per say,然后为类型选择relation
,向导将执行该过程并为您生成所有需要的代码!
An example of how it amazed me :一个让我惊讶的例子:
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.