我在使用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_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();
}
}
正如你所看到的,項目Id兩次設定,而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)
請記住,這些功能都位於表格/基/ BaseItemForm.class.php,但功能應該在/form/ItemForm.class.php中覆蓋。
我相信這是一個Propel錯誤,所以我將在明天發佈Propel TRAC。
更好地報告[Github]上的錯誤(https://github.com/propelorm/Propel)。 – j0k
相應的GitHub票證:[Ticket](https://github.com/propelorm/sfPropelORMPlugin/issues/143) –