I have 2 models I need to save to. In this instance, I do not need cakePHP to saveAll(), but want to save the content manually.
In my view I have the formHelper setup as “Model2.fieldname” and on post, if I do a pr(), there is an associative array created with [Model2] with the fields.
my code in the controller:
if($this->request->is("post") {
if($this->Model1->save($this->request->data) {
if($this->Model1->Model2->save($this->request->data) {
$this->Session->setFlash("Success");
}
}
}
It goes ahead and stores the data for model1, but fails on model2. It creates an empty record in the table because there is a created and modified table field, but the rest of the fields are blank. It also fails the validation for model2 (so it does not do any validation.)
Model1 hasOne Model2
I havent done this before so I am convinced I am doing something wrong, so if anyone can help thanks in advance 🙂
You can use cakephp’s
saveAssociated()method:Read about saveAssociated()