Accessing variable in controller method CAKEPHP -
students hasmany payments , payments belongsto student. when creating payment, have indicate student creating payment for. want able access id of student when payment being created, in order manipulate within add() method.
i have add() method in controller. here current code add().
public function add() { if ($this->request->is('post')) { $this->payment->create(); if ($this->payment->save($this->request->data)) { $this->session->setflash(__('the payment has been saved.')); return $this->redirect(array('action' => 'index')); } else { $this->session->setflash(__('the payment not saved. please, try again.')); } } $students = $this->payment->student->find('list'); $this->set(compact('students')); }
payment form code
<?php echo $this->form->create('payment'); ?> <fieldset> <legend><?php echo __('add payment'); ?></legend> <?php echo $this->form->input('student_id'); echo $this->form->input('date'); echo $this->form->input('total', array('default' => '0.0')); echo $this->form->input('notes'); ?> </fieldset> <?php echo $this->form->end(__('submit')); ?>
you should able access id as
$this->request->data['payment']['student_id']
so this:
public function add() { if ($this->request->is('post')) { $this->payment->create(); $student_id = $this->request->data['payment']['student_id']; // student id here... if ($this->payment->save($this->request->data)) { $this->session->setflash(__('the payment has been saved.')); return $this->redirect(array('action' => 'index')); } else { $this->session->setflash(__('the payment not saved. please, try again.')); } } $students = $this->payment->student->find('list'); $this->set(compact('students')); }
Comments
Post a Comment