2015-09-01 63 views
0

我有三個表行走1.6插入和updatin許多一對多的關係

1 - Items 
2 - Suppliers 
3 - Item_Suppliers 

三是項目和供應商之間的交叉引用表。

我的問題是如何保存和更新item_supplier表與推動。

在物品頁面上,客戶可以選擇多個供應商,然後將其發送到數據庫中保存。有什麼方法可以用默認的Propel 1.6函數將它保存到數據庫中。這就是數據來自用戶界面的方式。

array:8 [ 
    "Name" => "Strawberry" 
    "CategoryId" => "2" 
    "suppliers" => array:3 [ 
    0 => "1" 
    1 => "2" 
    2 => "3" 
    ] 
    "Points" => "20" 
    "Description" => "Strawberry Description" 
] 

回答

0

應設置架構中的isCrossRef="true"屬性爲您Item_Suppliers關係:

<table name="Item_Suppliers" isCrossRef="true"> 
    <column name="ItemId" type="integer" primaryKey="true"/> 
    <column name="SupplierId" type="integer" primaryKey="true"/> 
    <foreign-key foreignTable="Item" onDelete="CASCADE" onUpdate="CASCADE"> 
     <reference local="ItemId" foreign="Id"/> 
    </foreign-key> 
    <foreign-key foreignTable="Supplier" onDelete="CASCADE" onUpdate="CASCADE"> 
     <reference local="SupplierId" foreign="Id"/> 
    </foreign-key> 
</table> 

然後更新相關模型與生成的方法很簡單:

$suppliers = SupplierQuery::create()->filterById($input["supplier_ids"])->find(); 
$item->setSuppliers($suppliers)->save();