在 Propel 中命名生成的函数

发布于 2024-11-30 01:08:09 字数 1337 浏览 3 评论 0原文

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

上面,将向 Child 对象提供函数 getPersonRelatedByFatherId()getPersonRelatedByMotherId() >,很明显 getMother()getFather() 会工作得更好。可以编写自定义函数来执行此操作,但能够在架构中定义它会更有意义。

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

栀子花开つ 2024-12-07 01:08:09

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

Propel 根据模式中元素的 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,则使用表名称本身。

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

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>

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.

久夏青 2024-12-07 01:08:09

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

威廉

AFAIK it's not possible but Propel 1.6.3 generates better methods and fully integrates N-N relations and a fluent API is provided (setter/getter for collections for instance).

William

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文