1
votes

I'm using Symfony2 forms and have not been able to find out how to embed an intermediate class form that allows for creating a many to many "self-referencing" relationship of the type "parent/child". For example: a "Person" class that is related to itself through a "PersonParent" class. The xml schema would look like this:

<table name="person">
  <column name="id" type="INTEGER" primaryKey="true" autoIncrement="true" required="true"/>
  <column name="name" type="VARCHAR" size="100" required="true"/>
</table>
<table name="person_parent">
  <column name="person_id" type="INTEGER" primaryKey="true" required="true"/>
  <column name="parent_id" type="INTEGER" primaryKey="true" required="true"/>
  <foreign-key foreignTable="person" onDelete="CASCADE">
      <reference local="person_id" foreign="id"/>
  </foreign-key>
  <foreign-key foreignTable="person" onDelete="CASCADE">
      <reference local="parent_id" foreign="id"/>
  </foreign-key>
</table>

I'm using PropelBundle instead of doctrine, but even if you are using doctrine, you might be able to give me a lead on this issue. Please notice that the relationship at hand is not the same as a "Friend like" relationship for which there is the EqualNest behavior that creates methods to get/set "Friend" objects directly from a "Person" object form, without the need to embed the intermediate "PersonParentType" class form.

I tried to use Propel generated methods like setPersonParentsRelatedByPersonId, while embedding a PersonParentType form in the PersonType form to be able to add 'PersonParentsRelatedByPersonId' objects, but the problem is that I don't know how to define the "person_id" in the PersonParentType form since "person_id" is not yet available for a new Person. Adding only a field for the "parent_id" in the PersonParentType results in a "Not null violation" because the "person_id" is missing.

Any ideas? This question is related but different in scope to my earlier question where I inquired about the existence of a Propel behavior to handle this.

1

1 Answers

0
votes

You can find some documentation about selfreferencing manytomany with doctrine here http://doctrine-orm.readthedocs.org/en/2.0.x/reference/association-mapping.html#many-to-many-self-referencing