【问题标题】:Naming Generated Functions in Propel在 Propel 中命名生成的函数
【发布时间】:2011-10-28 19:46:56
【问题描述】:

cross-reference tables 的理念是在 Propel 1.5 中引入的。这意味着实体可以获得相关项目的列表,就好像它是一对多关系一样。所以在人到组的关系中,一个人可以调用getGroups(),一个组可以调用getPersons()

这使事情更容易处理。但是,如果实体与自身具有多对多关系,则函数调用的名称会变得更加复杂。例如,以下允许组在其内部包含组:

group:
  id: ~
  name: { type: varchar(255) }

sub_group:
  group_id:
    type: integer
    primaryKey: true
    foreignTable: group
    foreignReference: id
    required: true
  sub_group_id:
    type: integer
    primaryKey: true
    foreignTable: group
    foreignReference: id
    required: true

对于这种关系,Propel 生成了名称尴尬的函数getGroupsRelatedByGroupId()getGroupsRelatedBySubGroupId()。这些很长,而且不是很明显。作为这个实体的用户,我更喜欢使用getParentGroups()getSubGroups()这两个函数,我理解的更清楚。是否可以告诉 Propel 重命名这些函数? phpName 属性似乎没有这样做。

一对多关系也会出现问题,如下面非常简化的示例所示:

child:
  id: ~
  father_id:
    type: integer
    foreignTable: person
  mother_id:
    type: integer
    foreignTable: person

在上面,一个子对象将被赋予函数getPersonRelatedByFatherId()getPersonRelatedByMotherId(),而getMother()getFather() 应该会更好地工作。可以编写自定义函数来执行此操作,但能够在架构中定义它会更有意义。

【问题讨论】:

    标签: symfony1 many-to-many symfony-1.4 yaml propel


    【解决方案1】:

    解决方案就在 Propel 文档中,但直到今天我才注意到它:http://www.propelorm.org/documentation/04-relationships.html

    Propel 根据 schema 中元素的 phpName 属性生成 setAuthor() 方法。不设置属性时,Propel 使用相关表的 phpName 代替。

    使用您的第二个示例(在 XML 中,但转换为 YML 应该很简单):

    <table name="person">
      <column name="id" type="integer" required="true" primaryKey="true" autoIncrement="true" />
      <column name="father_id" type="integer" />
      <column name="mother_id" type="integer" />
      <foreign-key foreigntable="person" onDelete="setnull" phpName="father">
        <reference local="father_id" foreign="id" />
      </foreign-key>
      <foreign-key foreigntable="person" onDelete="setnull" phpName="mother">
        <reference local="mother_id" foreign="id" />
      </foreign-key>
    </table>
    

    注意“foreign-key”元素中的“phpName”属性,这是您设置自定义关系名称的地方。如果你把它留空(就像我今天之前一直做的那样),它将使用外部关系表的“phpName”,或者如果表上没有设置 phpName,则使用表名本身。

    【讨论】:

      【解决方案2】:

      AFAIK 这是不可能的,但 Propel 1.6.3 生成了更好的方法并完全集成了 N-N 关系并提供了流畅的 API(例如集合的 setter/getter)。

      威廉

      【讨论】:

        猜你喜欢
        • 2019-03-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-12-01
        • 2019-02-06
        • 2017-04-17
        • 1970-01-01
        • 2012-01-09
        相关资源
        最近更新 更多