Sorry, but you need to login to access this location.

Creating records for another model

Hi all,

I have a Student model which stores student details.

When a student record has been created, a fee payment record should be created in the FeePayment model. The FeePayment model contains records about how much each student is meant to pay and the status of the payment etc, so none of the data in the FeePayment record is created from a form it is meant to be generated automatically..

I know this is done in the add function of the student controller but not sure how to approach the syntax.

Appreciate any help.

Sid

Asked by saeed, on 19/7/10

Did you set a relation between Student Model and FeePayment model ?

mehlah - on 19/7/10

<< comments | comments >>

1 Answer

I suppose that a User hasOne FeePayment.

After setting this relation between your models, your controller code should be similar to this one :


<?php
function register() {
    if (!empty($this->data)) {
        // We can save the User data:
        // it should be in $this->data['User']
 
        $user = $this->User->save($this->data);

        // If the user was saved, Now we add this information to the data
        // and save the FeePayment.
      
        if (!empty($user)) {
            // The ID of the newly created user has been set
            // as $this->User->id.
            $this->data['FeePayment']['user_id'] = $this->User->id;
            $this->data['FeePayment']['value'] = $your_generated_fees;

            // Because our User hasOne FeePayment, we can access
            // the FeePayment model through the User model:
            $this->User->FeePayment->save($this->data);
        }
    }
}
?>

Understood ?

You should read the book to understand mechanisms

http://book.cakephp.org/view/84/Saving-Related-Model-Data-hasOne-hasMany-belongsTo

Thanks

Answered by mehlahon 19/7/10

Thanks but I'm having trouble. Student hasMany FeePayment. I've followed your template and made some changes but the other model is not being populated with data. I suspect it is because of the hasMany relationship between my models.

Here is my code;

`function add() {

if (!empty($this->data)) {

$this->Student->create();

if ($this->Student->save($this->data)) {

//debug($this->data, $showHTML = false, $showFrom = true);

//$feePayment = Array([FeePayment] => Array([student_id] => '1',[amount] => '100',[due_date] => '1/09/2010',[status] => 'Not paid'));

//$this->FeePayment->save($this->$feePayment);

// The ID of the newly created user has been set

// as $this->User->id.

$this->data['FeePayment'][0]['student_id'] = $this->Student->id;

$this->data['FeePayment'][0]['amount'] = '100';

$this->data['FeePayment'][0]['due_date'] = '01/01/2011';

$this->data['FeePayment'][0]['status'] = 'Unpaid';

// Because our User hasOne FeePayment, we can access

// the FeePayment model through the User model:

$this->Student->FeePayment->saveAll($this->data);

$this->Session->setFlash(__('The student has been saved', true));

$this->redirect(array('action' => 'index'));

} else {

$this->Session->setFlash(__('The student could not be saved. Please, try again.', true));

}`

saeed - on 20/7/10

<< comments | comments >>
<< previous next >>

Tagged with

Rating

0

Viewed

122 times

Last Activity

on 20/7/10