FuelPHP ORM bulk update related objects

I am trying to bulk update a table full of related items on an invoice.

$_POST           Existing relations   intended result
=====================================================
                 item_id: 3           unlink 3
item_id: 4       item_id: 4           update 4
item_id: 5       item_id: 5           update 5
item_id: null                         create x
item_id: null                         create x

I am pushing a json array of objects that have all of the $_properties the Model expects.

My Controller method looks like this:

public function update_items($invoice_id) 
{
    $invoice = Model_Invoice::find($invoice_id);

    if ($items = MyInput::json())
    {
        $invoice->from_array(array('invoice_items' => $items));
    }

    $invoice->save();
}

This gives a PK Collision error when saving the Related objects (invoice_items). This is because the objects that have been updated in the $invoice->invoice_items are all is_new() == true .

How should I be structuring this code? I don't want to have to manage add/edit/delete states for every incoming related item.


如果是更新,请确保您获取要更新的项目:

$invoice = Model_Invoice::find($invoice_id, array('related' => array('invoice_items')));

链接地址: http://www.djcxy.com/p/64810.html

上一篇: 只缓存来自FuelPHP ORM结果的关系数据

下一篇: FuelPHP ORM批量更新​​相关对象