Symfony2形式:如何处理多对多的“自我引用”类型“父/子”的关系 [英] Symfony2 forms: How to deal with many to many "self-referencing" relationships of the type "parent/child"?

查看:110
本文介绍了Symfony2形式:如何处理多对多的“自我引用”类型“父/子”的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Symfony2表单,并且无法找到如何嵌入中间类表单,允许创建父/子类型的多个自引用关系。例如:通过PersonParent类与自身相关的Person类。 xml模式如下所示:

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>

我使用PropelBundle而不是教义,但即使你使用的是原则,你也许可以给我这个问题的领先。请注意,手头的关系与Friend like关系不同,EqualNest行为创建了直接从Person对象形式获取/设置Friend对象的方法,而不需要嵌入中间的PersonParentType类形式。

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.

我尝试使用Propel生成的方法,如setPersonParentsRelatedByPersonId,同时在PersonType表单中嵌入PersonParentType表单,以便能够添加PersonParentsRelatedByPersonId对象,但问题是我不知道如何在PersonParentType表单中定义person_id,因为person_id尚不可用于新的Person。在PersonParentType中仅添加parent_id的字段会导致not null violation,因为person_id缺少。

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.

任何想法?这个问题是相关的,但范围与我之前询问的问题有所不同存在一个Propel行为来处理这个

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.

推荐答案

你可以在这里找到一些关于自我引用manytomany的文档 http:/ /doctrine-orm.readthedocs.org/en/2.0.x/reference/association-mapping.html#many-to-many-self-referencing

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

这篇关于Symfony2形式:如何处理多对多的“自我引用”类型“父/子”的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆