在 Propel 中命名生成的函数
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决方案就在 Propel 文档中,但直到今天我才注意到它: http: //www.propelorm.org/documentation/04-relationships.html
使用第二个示例(在 XML 中,但转换为 YML 应该很简单):
请注意“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
Using your second example (in XML, but the conversion to YML should be simple):
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.
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