Swamy
Swamy

Reputation: 400

How to send two different data to two different tables from two different actions in same controller in cakephp?

Class FramingPartnersController extends CroogoAppController {

public $components = array('Paginator', 'Session');
public $uses = array('FramingPartner');

public function index() {
    $test = 'This is Framing Partners Index';
    $this->set(compact('test'));
  }

public function profile() {
  if(!empty($this->request->data)){

    if ($this->request->is('post')) {
      $this->FramingPartner->create();
      if ($this->FramingPartner->save($this->request->data)) {
        $this->Session->setFlash(__d('croogo', 'The profile has been saved'), 'flash', array('class' => 'success'));
        $this->redirect(array('action' => 'frames'));
      } else {
        $this->Session->setFlash(__d('croogo', 'The profile could not be saved. Please, try again.'), 'default', array('class' => 'error'));
      }
    }
  }
}

  public function frames() {
    $test = 'This is Framing Partners frames';
    $this->set(compact('test'));
  }

}

I've two different tables each for profile and frames and want to send data to their respective table from the above mentioned actions. In short, data from profile should go to profile table and data from frame should go to frame table.

Upvotes: 1

Views: 74

Answers (1)

Swamy
Swamy

Reputation: 400

public function frames(){
    if(!empty($this->request->data)){
        if ($this->request->is('post')) {
            $this->loadModel('Frame');
            $this->Frame->create();
            if ($this->Frame->save($this->request->data)) {
                $this->Session->setFlash(__d('croogo', 'The profile has been saved'), 'flash', array('class' => 'success'));
                /*  $this->redirect(array('action' => 'frames'));*/
            } else {
                 $this->Session->setFlash(__d('croogo', 'The profile could not be saved. Please, try again.'), 'default', array('class' => 'error'));
              }
         }
     }
 }

In this way, I'm loading a different model from a different controller, i.e. loading Frame Model from FramingPartnersController.

Explicitly, I'm loading Frame Model which will automatically load Frame table.

Upvotes: 1

Related Questions