saveAll in one single Ajax call:
I send the data exactly the same, as in this discussion's first
message, with the non directly related model INSIDE the directly
related model (SectionParts INSIDE data[Section])
data[Denouncement][title]=asdf&
data[Section][0][user_given_name]=asdf&
data[Section][0][type]=6&
data[Section][0][SectionPart][0][name]=embed&
data[Section][0][SectionPart][0][value]=<object...object>&
data[Section][0][SectionPart][0][type]=embed&
data[Section][0][SectionPart][1][name]=top_left_img&
data[Section][0][SectionPart][1][value]=20090423T050438.jpg&
data[Section][0][SectionPart][1][type]=img&
data[Section][0][SectionPart][2][name]=bottom_left_img&
data[Section][0][SectionPart][2][value]=20090423T050439.jpg&
data[Section][0][SectionPart][2][type]=img&
data[Section][1][user_given_name]=my section name&
data[Section][1][type]=0&
data[Section][1][SectionPart][0][name]=title&
data[Section][1][SectionPart][0][value]=my value&
data[Section][1][SectionPart][0][type]=text&
data[Section][1][SectionPart][1][name]=text&
data[Section][1][SectionPart][1][value]=another value&
data[Section][1][SectionPart][1][type]=text
In my controller, I extract $this->data["Section"] from $this->data
and then use saveAll, then foreach Section saveAll its attributes and
SectionParts, here is my code:
---------------------------------------
CODE
---------------------------------------
function denouncement_save(){
$this->layout = null;
$user_id = $this->Session->read('Auth.User.id');
$this->data['Denouncement']['user_id'] = $user_id;
$sections_extracted_array = $this->data;
$sections_array = array_extract($sections_extracted_array,
"Section"); // Extracts data["Section"] from $this->data
$this->Denouncement->saveAll($sections_extracted_array); // Save
everything but Sections and SectionParts
$section_parts = array();
foreach($sections_array as $data){ // Save Sections & SectionParts
$section_parts = array_extract($data, "SectionPart");
$data["SectionPart"] = $section_parts;
$this->Denouncement->Section->saveAll($data);
}
$this->set('response', 'OK');
}
/**
* Extracts and Return the value of a specified key in and array.
**/
function array_extract(&$array_ref, $key_to_be_deleted)
{
$clean = array(); // The new array without the extracted element
$extracted = array(); // The extracted element(s)
if(!array_key_exists($key_to_be_deleted,$array_ref)) return;
foreach($array_ref as $key => $value) {
if($key != $key_to_be_deleted) {
$clean[$key] = $value;
}else{
$extracted = $value;
}
}
$array_ref = $clean;
return $extracted;
}
---------------------------------------
ENDS CODE
---------------------------------------
This worked fine for me, hopefully it will for someone else, at least
enlighten in the right path.
Regards
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---
No comments:
Post a Comment