1

I'm trying to update data about many shipments with one form.

Shipment model belongsTo Shippingaddress model and Shippingaddress hasMany Shipment.

In interface user can use his previously saved Shippingaddress from select field or fill small form and add new one.

Currently example structure of data which are posted to controller looks like this:

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',
            )
        )
    )

It would be nice to save Shiippingaddress data and update all Shipments in one transaction with

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

Is it possible?

I was trying many combination of posted array structure but without success.

1 Answer 1

0

Your $data structure should looks like this:

$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'
            )
        ),
    )
);

For save use saveMany:

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

You don't need to specify shippingadress_id to empty string because Cake already takes care of it and put all relevant foreign keys based on the relationship of your models.

Not the answer you're looking for? Browse other questions tagged or ask your own question.