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

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -