【问题标题】:Symfony2 + Doctrine : Tool for generating relations between entitiesSymfony2 + Doctrine:生成实体之间关系的工具
【发布时间】:2013-04-07 03:25:31
【问题描述】:

当我想创建一个新实体时,我使用以下命令:

php 应用程序/控制台原则:生成实体

但这只会帮助我生成带有 setter 和 getter 的属性

有没有一种方法可以帮助我生成实体之间的关系(例如 OneToMany 和 ManyToOne)?

【问题讨论】:

  • 你可以在你的IDE中配置模板/宏,我觉得这样比较简单。

标签: php symfony orm doctrine-orm


【解决方案1】:

你可以试试 ORM Designer。它不便宜,但很有帮助:

http://www.orm-designer.com

【讨论】:

  • ORM Designer 已重命名为 Skipper,现在可在 skipper18.com 上使用。
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 2014-11-20
    • 1970-01-01
    • 2012-07-23
    • 1970-01-01
    • 2012-06-23
    相关资源
    最近更新 更多