Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More accurate generated collection methods for (multi-)self-referenced many-to-many (crossRef) relationships #1212

Open
gossi opened this issue Jun 9, 2016 · 1 comment

Comments

@gossi
Copy link
Contributor

gossi commented Jun 9, 2016

Hey guys,

I do have a table that is referenced many-to-many multiple times, here is the relevant schema:

<database name="default">

  <table name="skill">
        <column name="id" type="INTEGER" primaryKey="true" required="true" autoIncrement="true" />
        <column name="name" type="VARCHAR" size="255"/>
  </table>

  <table name="skill_dependency" isCrossRef="true">
        <!-- dependency_id depends on parent_id -->
        <column name="dependency_id" type="INTEGER" primaryKey="true" required="true" />
        <column name="parent_id" type="INTEGER" primaryKey="true" required="true" />

        <foreign-key foreignTable="skill" onDelete="cascade" name="skill_dependency_fk_skill" refPhpName="Child">
            <reference local="dependency_id" foreign="id"/>
        </foreign-key>
        <foreign-key foreignTable="skill" onDelete="cascade" name="skill_dependency_fk_parent" refPhpName="Parent">
            <reference local="parent_id" foreign="id" />
        </foreign-key>
    </table>

    <table name="skill_part" isCrossRef="true">
        <!-- part_id is a part of composite_id -->
        <column name="part_id" type="INTEGER" primaryKey="true" required="true" />
        <column name="composite_id" type="INTEGER" primaryKey="true" required="true" />

        <foreign-key foreignTable="skill" onDelete="cascade" name="skill_part_fk_part" refPhpName="Part">
            <reference local="part_id" foreign="id" />
        </foreign-key>
        <foreign-key foreignTable="skill" onDelete="cascade" name="skill_part_fk_composite" refPhpName="Composite">
            <reference local="composite_id" foreign="id" />
        </foreign-key>
    </table>

</database>

This will give me the following methods:

  • Skill::addParent(SkillDependency)
  • Skill::addChild(SkillDependency)
  • Skill::addPart(SkillPart)
  • Skill::addComposite(SkillPart)
  • Skill::addSkillRelatedByDependencyId(Skill)
  • Skill::addSkillRelatedByParentId(Skill)
  • Skill::addSkillRelatedByPartId(Skill)
  • Skill::addSkillRelatedByCompositeId(Skill)

which is pretty much the opposite of what I want but correct according to the current name generating strategy. So, I was wondering, that in case of a cross-ref table, the name generation strategy could be twisted to this:

  1. Detect ref-name:
    1. Check for refPhpName on the <foreign-key> element on the crossRef table
    2. Use local attribute on nested <reference> element
  2. Generate collection methods with ref-name (add/get/remove/set)
  3. Generate methods that related to the cross-ref table (with scheme <CrossRefTableName>RelatedBy<RefName>(), e.g. Skill::addSkillDependencyByParent(SkillDependency))

Which would generate these methods:

  • Skill::addParent(Skill)
  • Skill::addChild(Skill)
  • Skill::addComposite(Skill)
  • Skill::addPart(Skill)
  • Skill::addSkillDependencyRelatedByParent(SkillDependency)
  • Skill::addSkillDependencyRelatedByChild(SkillDependency)
  • Skill::addSkillPartRelatedByPart(SkillPart)
  • Skill::addSkillPartRelatedByComposite(SkillPart)

They would feel much more "natural" (as if you would do it by hand).

@dereuromark
Copy link
Contributor

Is someone able to make a PR here with suggested changes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants