Adding related data to the :: beforeSave model
I am trying to add related data during cakePHP beforeSave () but it looks like cakePHP is ignoring it.
<?php
App::uses("AppModel", "Model");
class Recipe extends AppModel {
public $hasMany = array('Ingredient');
public function beforeSave($options=array()) {
if(!isset($this->data["Ingredient"])) {
Debugger::log("data[Ingredient] didn't exist! adding some ingredients...");
$this->data["Ingredient"] = array(
array(
'Ingredient' => array(
'name' => 'Gluten Free Flour',
'amount' => '12 cups'
)
),
array(
'Ingredient' => array(
'name' => 'Sugar',
'amount' => '50 cups'
)
),
array(
'Ingredient' => array(
'name' => 'A shoe',
'amount' => 'Just one'
)
),
);
}
Debugger::log("Saving Data: ");
Debugger::log($this->data, 7, 10);
return true;
}
}
In mine RecipesController
I have this:
public function test_save() {
//Test saving With the ingredients in the recipe
Debugger::log("Saving w/ ingredients");
Debugger::log($this->Recipe->saveAssociated(array(
'Recipe' => array(
'name' => 'Test Recipe 1'
),
'Ingredient' => array(
array(
'name' => 'Test Ingredient 1 for Test Recipe 1',
'amount' => 'a few'
),
array(
'name' => 'Test Ingredient 2 for Test Recipe 1',
'amount' => 'a lot'
)
)
)));
Debugger::log("Saving w/o ingredients");
Debugger::log($this->Recipe->saveAssociated(array(
'Recipe' => array(
'name' => 'Test Recipe 2'
)
)));
}
Saving with ingredients works, but saving without ingredients does not work, the data in Recipe-> data before is beforeSave
returned:
array(
'Recipe' => array(
'name' => 'Test Recipe 1'
),
'Ingredient' => array(
(int) 0 => array(
'Ingredient' => array(
'name' => 'Test Ingredient 1 for Test Recipe 1',
'amount' => 'a few'
)
),
(int) 1 => array(
'Ingredient' => array(
'name' => 'Test Ingredient 2 for Test Recipe 1',
'amount' => 'a lot'
)
)
)
)
And when Ingredient is added before saving:
array(
'Recipe' => array(
'name' => 'Test Recipe 1'
),
'Ingredient' => array(
(int) 0 => array(
'Ingredient' => array(
'name' => 'Gluten Free Flour',
'amount' => '12 cups'
)
),
(int) 1 => array(
'Ingredient' => array(
'name' => 'Sugar',
'amount' => '50 cups'
)
),
(int) 2 => array(
'Ingredient' => array(
'name' => 'A shoe',
'amount' => 'Just one'
)
)
)
)
When the data is in the array before I call the save method, it will only work fine when I try to add the associated data to the beforeSave.
source to share
From the documentation:
Make sure beforeSave () returns true or your save will work.
http://book.cakephp.org/2.0/en/models/callback-methods.html#beforesave
Edited: I checked your code and you are correct, related data added to beforeSave()
is ignored. This is because you cannot add or modify related data in the model beforeSave()
. This means that in your Recipe beforeSave()
you can neither add ingredients nor change ingredients that you previously attached to the recipe in your controller.
In a Recipe, beforeSave()
you can only change the Recipe data.
This is mistake? According to Mark Storey (creator of CakePHP), this is not the case:
Its a function, as currently saveAll allows you to use beforeSave to change the record you want to save, not the complete dataset . I personally don't feel that this should change, but this is something that could be discussed.
This statement applies to saveAll()
, but the important part is what he said about beforeSave()
. Check out the full version: https://github.com/cakephp/cakephp/issues/1765
source to share