cakephp - Adding associated data in model::beforeSave -
i'm trying add together associated info during cakephps beforesave() looks cakephp 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 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); homecoming true; } }
in recipescontroller
have this:
public function test_save() { //test saving ingredients in 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 test recipe 1', 'amount' => 'a few' ), array( 'name' => 'test ingredient 2 test recipe 1', 'amount' => 'a lot' ) ) ))); debugger::log("saving w/o ingredients"); debugger::log($this->recipe->saveassociated(array( 'recipe' => array( 'name' => 'test recipe 2' ) ))); }
the save ingredients works, save without ingredients doesn't, info in recipe->data before beforesave
returns this:
array( 'recipe' => array( 'name' => 'test recipe 1' ), 'ingredient' => array( (int) 0 => array( 'ingredient' => array( 'name' => 'test ingredient 1 test recipe 1', 'amount' => 'a few' ) ), (int) 1 => array( 'ingredient' => array( 'name' => 'test ingredient 2 test recipe 1', 'amount' => 'a lot' ) ) ) )
and when ingredient added in before save:
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 info in array before phone call save method, it'll work fine, when seek , add together associated info in beforesave.
from documentation:
be sure beforesave() returns true, or save going fail.
http://book.cakephp.org/2.0/en/models/callback-methods.html#beforesave
edited: tested code , right, related info added in beforesave()
beingness ignored. happening because cannot add together or modify related info in model's beforesave()
. means that, in recipe's beforesave()
, cannot neither add together ingredients nor modify ingredients attached recipe in controller.
in recipe's beforesave()
can modify recipe data.
is bug? not, according mark story (creator of cakephp):
its feature, saveall only lets utilize beforesave modify record that's saved, not total info set. don't sense should change, discussed.
this statement saveall()
of import part said beforesave()
. check total thread: https://github.com/cakephp/cakephp/issues/1765
cakephp
No comments:
Post a Comment