1

Students HasMany Payments and Payments BelongsTo Student. When creating a Payment, I have to indicate which Student I am creating this Payment for. I want to be able to access the id of the Student when the Payment is being created, in order to manipulate something within the add() method.

I have an add() method in my controller. Here is the current code for the 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 could not be 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')); ?>
1
  • Can you add the code for the Payment form in the view? If you are selecting a student in that form then it should be included in $this->request->data Commented Feb 6, 2014 at 4:29

2 Answers 2

2

You should be able to access the ID as

$this->request->data['Payment']['student_id']

So something like this:

public function add() {     
    if ($this->request->is('post')) {
        $this->Payment->create();
        $student_id = $this->request->data['Payment']['student_id'];
        // Do something with 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 could not be saved. Please, try again.'));
        }
    }
    $students = $this->Payment->Student->find('list');
    $this->set(compact('students'));
}
Sign up to request clarification or add additional context in comments.

Comments

1

One strategy I find very useful for navigating CakePHP's large multi-dimensional arrays is to use the debug() function often in development.

For example, in the add() method I would do something like:

if ($this->request->is('post')) {
    debug($this->request->data);
    die;
}

Then you'll be able to see where that Student id is hiding and use it however you need before the add() method finishes. I don't know the exact structure your array will be in, but most likely you should be able to do something like:

$student_id = $this->request->data['Payment']['Student']['id'];

Just check the output of the debug() first (after submitting the form) to determine where in the array the data you want has been placed.

1 Comment

Thank you! I'll be sure to use this in the future. got it working now

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.