13

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 Image_one

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

Image_two

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

Image_three

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.

A.A Noman
  • 5,244
  • 9
  • 24
  • 46
  • Turn `$this->request->data` into an array, which can hold both, currently your just overwriting it. – Lawrence Cherone Sep 16 '17 at 12:55
  • I am not clear your comment. Please explain more – A.A Noman Sep 16 '17 at 13:01
  • In your code you're doing `$this->request->data = $site_id_1; $this->request->data = $site_id_2;` which will overwrite the first with the second. if it were `$this->request->data['site_1'] = $site_id_1; $this->request->data['site_2'] = $site_id_2;` you could then loop it into the right model. – Lawrence Cherone Sep 16 '17 at 13:03
  • would you suggest me like this 'if (!$this->request->data) {$this->request->data[0] = $site_id_1;$this->request->data[1] = $site_id_2;}' – A.A Noman Sep 16 '17 at 13:38
  • you must use array of data => $this->request->data['site_1'] = $site_id_1; $this->request->data['site_2'] = $site_id_2; – Honarkhah Sep 19 '17 at 12:20
  • It's pretty sound. But what portion change of my code, explain please – A.A Noman Sep 19 '17 at 13:13

2 Answers2

8

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.

Daniel W
  • 433
  • 3
  • 8
6

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');
Ahmed Ginani
  • 6,522
  • 2
  • 15
  • 33