同桌上的多对多关系

时间:2012-06-06 09:09:44

标签: symfony1 many-to-many symfony-1.4 propel

我使用Symfony 1.4和Propel ORM在同一张桌子上有很多关系。我的schema.yml看起来像这样:

propel:
  item:
    _attributes: { phpName: Item }
    id: { phpName: Id, type: INTEGER, size: '10', primaryKey: true, autoIncrement: true, required: true }
    type_id: { phpName: TypeId, type: INTEGER, size: '11', required: true, foreignTable: type, foreignReference: id, onDelete: RESTRICT, onUpdate: RESTRICT }
    owner_id: { phpName: OwnerId, type: INTEGER, size: '11', required: true, foreignTable: owner, foreignReference: id, onDelete: RESTRICT, onUpdate: RESTRICT }
    place_id: { phpName: PlaceId, type: INTEGER, size: '11', required: true, foreignTable: place, foreignReference: id, onDelete: RESTRICT, onUpdate: RESTRICT }
    picture_id: { phpName: PictureId, type: INTEGER, size: '11', required: false, foreignTable: picture, foreignReference: id, onDelete: RESTRICT, onUpdate: RESTRICT }
    supplier_id: { phpName: SupplierId, type: INTEGER, size: '11', required: false, foreignTable: supplier, foreignReference: id, onDelete: RESTRICT, onUpdate: RESTRICT }
    name: { phpName: Name, type: VARCHAR, size: '255', required: true }
    amount: { phpName: Amount, type: INTEGER, size: '11', required: true }
    wished_amount: { phpName: WishedAmount, type: INTEGER, size: '11', required: true }
    costs: { phpName: Costs, type: DECIMAL, size: '7', scale: '2', required: true }
    description: { phpName: Description, type: LONGVARCHAR, required: false }
    use_until: { phpName: UseUntil, type: DATE, required: false }
    last_used: { phpName: LastUsed, type: TIMESTAMP, required: false }
    last_updated: { phpName: LastUpdated, type: TIMESTAMP, required: false }
    _indexes: { item_FI_1: [type_id], item_FI_2: [owner_id], item_FI_3: [place_id], item_FI_4: [picture_id], item_FI_5: [supplier_id] }

 item_has_item:
    item_id: { type: INTEGER, required: true, foreignTable: item, foreignAlias: item, foreignReference: id, primaryKey: true}
    parent_item_id: { type: INTEGER, required: true, foreignTable: item, foreignAlias: parent_item, foreignReference: id, primaryKey: true}
    _indexes: { item_has_item_FKIndex1: [item_id], item_has_item_FKIndex2: [parent_item_id] }

但是,管理生成器中所需的admin_double_list不会自动显示。当第二个foreignTable调整到另一个表格时,例如sf_guard_user。我已经读过,这可以在Propel 1.3中修复,但我不知道Symfony 1.4中包含哪个Propel版本。

我希望我能为你解决我的问题提供足够的信息。否则,我将不得不制作另一个包含子项的项目的表,如下所示:

  

Item< ---- Item_has_item< ---- Combined_item

从j0k反应后编辑

在j0k的反应之后,我确实更新了sfPropelORMPlugin,它确实识别了这种关系。但是,我在保存时仍然出错(仅在建立关系时):

Unable to execute INSERT statement [INSERT INTO `item_has_item` (`ITEM_ID`) VALUES (:p0)]
[wrapped: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a 
child row: a foreign key constraint fails (`SI/item_has_item`, CONSTRAINT 
`item_has_item_FK_1` FOREIGN KEY (`parent_item_id`) REFERENCES `item` (`id`))]

经过长时间的广泛搜索后,我发现表单生成器无法正确理解同一个表上的多对多关系。在函数saveItemHasItemList($ con = null)中:

$c = new Criteria();
$c->add(ItemHasItemPeer::ITEM_ID, $this->object->getPrimaryKey());
ItemHasItemPeer::doDelete($c, $con);

$values = $this->getValue('item_has_item_list');
if (is_array($values))
    {
      foreach ($values as $value)
      {
        $obj = new ItemHasItem();
        $obj->setItemId($this->object->getPrimaryKey());
        $obj->setItemId($value);
        $obj->save();
      }
    }

如您所见,ItemId设置了两次,而ParentItemId未设置,因此约束值。它应该生成为:

$c->add(ItemHasItemPeer::ITEM_ID, $this->object->getPrimaryKey());
$c->add(ItemHasItemPeer::PARENT_ITEM_ID, $this->object->getPrimaryKey());

&安培;&安培;

$obj->setParentItemId($this->object->getPrimaryKey());
$obj->setItemId($value);

这解决了保存问题,但是,它仍然没有显示已经选择的关系。要解决这个问题,您还应该更改updateDefaultsFromObject():

foreach ($this->object->getItemHasItemsRelatedByItemId() as $obj)

应该是:

foreach ($this->object->getItemHasItemsRelatedByParentItemId() as $obj)

请记住,这些函数位于form / base / BaseItemForm.class.php中,但这些函数应该在/form/ItemForm.class.php中覆盖。

我相信这是一个Propel bug,所以我明天会在Propel TRAC上发布。

1 个答案:

答案 0 :(得分:0)

如果您使用的是最后一个symfony 1.4,则使用Propel 1.4.2

您应该尝试使用Pro new version的Propel插件。它使用Propel 1.6。