I have two Models Group_ones
and Group_twos
. I show this value in ac_config.ctp
file.
My controller code is below
public function ac_config($id = null)
{
if (!$id) {
$this->Session->setFlash('Please provide a Site id');
$this->redirect(array('action'=>'dashboard'));
}
$site_id_1 = $this->GroupOne->findById($id);
$site_name = $site_id_1['GroupOne']['site_name'];
$ac_one_time = $site_id_1['GroupOne']['ac_on_time_one'];
$group_one_active = $site_id_1['GroupOne']['active'];
$site_id_2 = $this->GroupTwo->findById($id);
$ac_two_time = $site_id_2['GroupTwo']['ac_on_time_two'];
$group_two_active = $site_id_2['GroupTwo']['active'];
if (!$site_id_1) {
$this->Session->setFlash('Invalid Site ID Provided');
$this->redirect(array('action'=>'dashboard'));
}
if (!$site_id_2) {
$this->Session->setFlash('Invalid Site ID Provided');
$this->redirect(array('action'=>'dashboard'));
}
if ($this->request->is('post') || $this->request->is('put')) {
$this->GroupOne->id = $id;
$this->GroupTwo->id = $id;
if (($this->GroupTwo->save($this->request->data)) || ($this->GroupOne->save($this->request->data))) {
$this->Session->setFlash(__('AC Configuration has been update'));
$this->redirect(array('action' => 'ac_config', $id));
}
else
{
$this->Session->setFlash(__('Unable to AC Configuration has been update.'));
}
}
$this->set(compact('site_name','ac_one_time','group_one_active'));
$this->set(compact('ac_two_time','group_two_active'));
$this->set('group_one', $site_id_1);
$this->set('group_two', $site_id_2);
if (!$this->request->data) {
$this->request->data = $site_id_1;
$this->request->data = $site_id_2;
}
}
My view content is below
Problem occurs when I save
button. It only save group-2 form values in database. When AC Run Time
and AC Rest Time
show Group-2
then form values save in database of the Model Group_twos
and AC Run Time
and AC Rest Time
show Group-1
then form values save in database of the Model Group_ones
.
Another problem is below
if (!$this->request->data) {
$this->request->data = $site_id_1;
$this->request->data = $site_id_2; // this show rest time, run time
}
If I write this above code then I get
if (!$this->request->data) {
$this->request->data = $site_id_2;
$this->request->data = $site_id_1; //this show rest time, run time.
}
Again if I write this above code then I get
But I need AC Run Time and AC Rest Time
both group same time and when I click save button any two group that corresponds group value save in Model. Whats my wrong my logic.
Thanks your help.
Simply merge the two arrays before setting the request data:
if (!$this->request->data) {
$this->request->data = array_merge($site_id_1, $site_id_2);
}
If this does not help, it would be useful to see the code of your view as well.
As per your code, you are overwriting the request data array. what you need to do is that you have to set both arrays separately or by merging in the single array.
Below your current code overwriting $site_id_1
by $site_id_2
:
$this->request->data = $site_id_1;
$this->request->data = $site_id_2;
Now you can pass data below ways:
$this->request->data['site_id_1'] = $site_id_1;
$this->request->data['site_id_2'] = $site_id_2;
or
$this->request->data = array_merge($site_id_1, $site_id_2);
You just have to change those two lines.
Also, I don't know how you fetching the result in view for both models but you can fetch data separately something like below:
$site_id_1 = $this->request->getData('site_id_1');
$site_id_2 = $this->request->getData('site_id_2');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With