Ticket #23 (closed enhancement: fixed)

Opened 5 years ago

Last modified 12 months ago

Add possibility to define own method names for using foreign key relationships

Reported by: maulin@… Owned by: hans
Priority: normal Milestone: 1.3
Component: Generator Version: devel
Severity: trivial Keywords:
Cc:

Description (last modified by hans) (diff)

naming of foreignkey relations (ie one:many or many:one relationships) is a little kludgy. It works fine for the usual case, where you are relating to a foreign table that maps to exactly one object. but in many cases, this is not the case. i added a "phpname" attribute to the foreign key in the schema.xml, and then created some wrapper functions in Object.tpl that make them prettier.

example:

old:

<table name="foo"...>
<column name="positionId" ...>
<column name="groupId" ...>
<foreign-key table="code">
<reference local="positionId" foreign="id"/>
</foreign-key>
<foreign-key table="code">
<reference local="groupId" foreign="id"/>
</foreign-key>
</table>

setCodeRelatedByPositionId () setCodeRelatedByGroupId ()

with the addition of phpname to foreignkey...

<foreign-key table="code" phpname="position">
<reference local="positionId" foreign="id"/>
</foreign-key>
<foreign-key table="code" phpname="group">
<reference local="groupId" foreign="id"/>
</foreign-key>

now generates:

setPosition() setGroup()

which call the above (ugly named) functions.

pretty easy to add, and makes the api cleaner.

Change History

Changed 5 years ago by hans

  • status changed from new to assigned
  • description modified (diff)

This sounds like a very useful addition. Thanks.

Changed 5 years ago by david

  • summary changed from enhancement to Add possibility to define own method names for using foreign key relationships

Changed 4 years ago by soenke

  • type changed from defect to enhancement
  • milestone changed from 1.2 to 1.3

Changed 3 years ago by hans

  • status changed from assigned to closed
  • resolution set to fixed

Fixed in [494] for 1.3 branch of Propel.

Support was added for a phpName and refPhpName attributes in the <foreign-key> element. For example:

<!-- test self-referencing foreign keys and inheritance-->
  <table name="bookstore_employee">
        <column
    name="id"
    type="INTEGER"
    primaryKey="true"
    autoIncrement="true"
    description="Employee ID number"/>
  <column
    name="name"
    type="VARCHAR"
    size="32"
    description="Employee name"/>
  <column
    name="job_title"
    type="VARCHAR"
    size="32"
    description="Employee job title"/>
  <column
    name="supervisor_id"
    type="INTEGER"
    description="Fkey to supervisor."/>
  <foreign-key foreignTable="bookstore_employee" 
                     phpName="Supervisor" 
                     refPhpName="Subordinate">
    <reference local="supervisor_id" foreign="id"/>
  </foreign-key>
  </table>

This is an example from a self-referencing table in bookstore schema.xml.

The phpName attribute affects naming of methods like setSupervisor(), while the refPhpName attribute affects naming of methods that work with the referencing foreign keys -- e.g. addSubordinate(), getSubordinates().

Note: See TracTickets for help on using tickets.