1

我正在尝试使用一种表格更新有关许多货物的数据。

Shipment模型belongsTo Shippingaddress模型和Shippingaddress hasMany Shipment

在界面中,用户可以从选择字段中使用他以前保存的送货地址或填写小表格并添加新的。

当前发布到控制器的数据示例结构如下所示:

array(
        (int) 0 => array(
            'Shipment' => array(
                'id' => '223',
                'shippingaddress_id' => '8',
            )
        ),
        (int) 1 => array(
            'Shipment' => array(
                'id' => '224',
                'shippingaddress_id' => '',
            ),
            'Shippingaddress' => array(
                'recipient' => 'some name',
                'address' => 'some addres data',
                'zip' => '00-555',
                'city' => 'some city',
            )
        ),
        (int) 2 => array(
            'Shipment' => array(
                'id' => '225',
                'shippingaddress_id' => '12',
            )
        )
    )

保存 Shiippingaddress 数据并在一次交易中更新所有 Shipments 会很好

$this->Shipment->saveAll($data).

可能吗?

我尝试了许多张贴数组结构的组合,但没有成功。

4

1 回答 1

0

您的$data结构应如下所示:

$data = array(
    'Shippingadress' => array(
        '0' => array(
            'id' => '8',
            'Shipment' => array(
                'id' => '223'
            )
        ),
        '1' => array(
            'recipient' => 'some name',
            'address' => 'some addres data',
            'zip' => '00-555',
            'city' => 'some city'
            'Shipment' => array(
                'id' => '224'
            )
        ),
        '2' => array(
            'id' => '12',
            'Shipment' => array(
                'id' => '255'
            )
        ),
    )
);

为了保存使用 saveMany:

$this->Shippingadress->saveMany($data, array('deep' => true) );

您无需指定shippingadress_id为空字符串,因为 Cake 已经处理好它并根据模型的关系放置所有相关的外键。

于 2013-07-10T22:38:58.550 回答