在Propel中命名生成的函数 [英] Naming Generated Functions in Propel

查看:60
本文介绍了在Propel中命名生成的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

交叉引用表的思想是在Propel 1.5中引入.这意味着实体可以获取相关项目的列表,就好像它是一对多关系一样.因此,在人与组的关系中,一个人可以呼叫getGroups(),一个组可以呼叫getPersons().

The ideo of cross-reference tables is introduced in Propel 1.5. This means that an entity can get a list of related items as if it were a one-to-many relation. So in a person-to-groups relationship, a person can call getGroups(), and a group can call getPersons().

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

This makes things much easier to handle. However, if an entity has a many-to-many relationship to itself, the names of the function calls become more complex. As an example, the following permits groups to contain groups within themselves:

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属性似乎没有执行此操作.

For this relationship, Propel generates the awkwardly named functions getGroupsRelatedByGroupId() and getGroupsRelatedBySubGroupId(). These are long and not immediately obvious. As a user of this entity, I would much prefer to use the functions getParentGroups() and getSubGroups(), which I can understand more clearly. Is it possible to tell Propel to rename these functions? The phpName attribute doesn't seem to do this.

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

The problem also occurs with one-to-many relations, as in the very simplified example below:

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

上面,当立即很明显getMother()getFather()会更好地工作时,将为Child对象赋予函数getPersonRelatedByFatherId()getPersonRelatedByMotherId().可以编写执行此操作的自定义函数,但是能够在架构中定义它更有意义.

Above, a Child object will be given the functions getPersonRelatedByFatherId() and getPersonRelatedByMotherId(), when it should be immediately obvious that getMother() and getFather() would work better. It's possible to write custom functions that do this, but it would make much more sense to be able to define it in the schema.

推荐答案

解决方案就在Propel文档中,但直到今天我才注意到:

The solution is right here in the Propel docs but I've never noticed it until today: http://www.propelorm.org/documentation/04-relationships.html

Propel基于架构中元素的phpName属性生成setAuthor()方法.如果未设置该属性,则Propel会使用相关表的phpName.

Propel generates the setAuthor() method based on the phpName attribute of the element in the schema. When the attribute is not set, Propel uses the phpName of the related table instead.

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

Using your second example (in XML, but the conversion to YML should be simple):

<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>

请注意外键"元素中的"phpName"属性,即在其中设置自定义关系名称的位置.如果您将其保留为空白(就像今天以前一样),它将使用外部关系表的"phpName",如果未在表上设置phpName,则将使用表名本身.

Notice the "phpName" attribute in the "foreign-key" element, that is where you set your custom relationship name. If you leave it blank (as I always did before today) it will use the "phpName" of the foreign relation table, or the table name itself if the phpName is not set on the table.

这篇关于在Propel中命名生成的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆