2
votes

I'm currently using cakephp 2.2.3.

I have the following Model Associations:

VehicleModel -> Vehicle -> Order

Plan -> Order

Vehicle HABTM Tag

Inside the Vehicle controller, add action, I have:

if(!empty($this->request->data)) {

    if($this->Vehicle->saveAll($this->request->data)) {

        $this->Session->setFlash('Vehicle was successfully added.');
    }

}

The $this->request->data array is formatted like this:

array(
    'VehicleModel' => array(
        'category_id' => '2',
        'make_id' => '1'
    ),
    'Order' => array(
        'plan_id' => '2'
    ),
    'Vehicle' => array(
        'vehicle_model_id' => '13',
        'price' => ' 8700',
        'year' => '1994',
        'km' => '100',
        'color' => '61',
        'fuel' => '1',
        'gear' => '20',
        'type' => '51',
        'city' => 'Rio de Janeiro',
        'state' => 'RJ'
    ),
    'Tag' => array(
        'Tag' => array(
            (int) 0 => '69',
            (int) 1 => '11'
        )
    )
)

The orders table has the following fields:

id , plan_id , vehicle_id , created , modified.

Vehicle Model:

class Vehicle extends AppModel {

    public $belongsTo = array('User' , 'VehicleModel');
    public $hasMany = array('Order' , 'Image');
    public $hasAndBelongsToMany = array('Accessory' , 'Tag');
}

Order Model:

class Order extends AppModel {

    public $belongsTo = array('Vehicle' , 'Part' , 'Plan');

    public $validate = array(
        'plan_id' => array(
            'rule' => 'notEmpty'
        )
    );

}

The problem I'm having is that the Order.plan_id field is not being saved, although all other fields are being saved normally. What can I be doing wrong?

1
Just to be clear, When I do the multiple saving manually, everything works just fine. I mean, when I write: $this->Vehicle->save(), and then set $this->request->data['Order']['vehicle_id'] = $this->Vehicle->id, and finally $this->Vehicle->Order->save(), everything works just fine. It's the saveAll that is causing me trouble. - Brunno Vodola Martins
Can you post the models Vehicle/Order? - iexiak
In older versions of cake, if the main model data was not first in the data array it would act inconsistently - what version of cake are you using? - AD7six
I have added the Vehicle/Order models. As for the version I'm using, it's 2.2.3. - Brunno Vodola Martins

1 Answers

1
votes

Just to be clear, When I do the multiple saving manually, everything works just fine. I mean, when I write:

$this->Vehicle->save()

and then set

$this->request->data['Order']['vehicle_id'] = $this->Vehicle->id

and finally

$this->Vehicle->Order->save()

everything works just fine. It's the saveAll that is causing me trouble.

If that is the case, see where $this->request->data['Order']['vehicle_id'] = $this->Vehicle->id. Comparing this to your var dump above, order never contains the relation to the main model, which leads me to ask if this is a new record you are trying to save or an update? I think you might have to not go with a saveAll here if you are setting a new record because the main id is not yet set. Please see:

http://book.cakephp.org/2.0/en/models/saving-your-data.html#saving-related-model-data-hasone-hasmany-belongsto

Particularly: "If neither of the associated model records exists in the system yet (for example, you want to save a new User and their related Profile records at the same time), you’ll need to first save the primary, or parent model."

They basically do the long version you are doing.