Saturday, April 12, 2014

Associations set null

 

I have several tables and corresponding models, that is to say, Staffs, Dpmembers, Subjects and Positions tables. In my Staff model I create hasOne on Department because I want to retrieve data from Department table which is working.

But I have also created more associations of hasMany on Dpmember, Subject and Position models because I want to save the corresponding staff records.

The view newstaff.ctp looks like this

<div class="staff form">    <?php echo $this->Form->create('Staff');?>  <h3><?php echo __('Add a new staff member'); ?></h3>  <?php     echo $this->Form->input('name');  echo $this->Form->input('marital',array('label'=>'Marital status','options'=>array('empty'=>'Choose status','Single'=>'Single','Divorced'=>'Divorced','Married'=>'Married')));  echo $this->Form->input('Children');  echo $this->Form->input('nationality');  echo $this->Form->input('location');  echo $this->Form->input('email');     echo $this->Form->input('phone',array('label'=>'Phone number'));  echo $this->Form->input('nextofkeen',array('label'=>'Next of keen'));  echo $this->Form->input('keenrelation',array('label'=>'Next of keen relationship','options'=>array('Choose option'=>'Choose option','Husband'=>'Husband','Wife'=>'Wife','Guardian'=>'Gaurdian','Child'=>'Child')));  echo $this->Form->input('school');  echo $this->Form->input('award');  echo $this->Form->input('schoolperiod');  echo $this->Form->input('workplace',array('label'=>'Workplace'));  echo $this->Form->input('workposition');  echo $this->Form->input('workperiod');  echo $this->Form->input('dpmember.department.',array('options'=>$department,'empty'=>'Choose Department','label'=>'Department'));  echo $this->Form->input('subject.subjcet',array('options'=>array('Choose option'=>'Choose option','Science'=>'Science','Social Studies'=>'Social studies','English'=>'English','Mathematics'=>'Mathematics'),'label'=>'Subject'));  echo $this->Form->input('position.role',array('options'=>array('Choose option'=>'Choose option','Class teacher'=>'Class teacher','Bursar'=>'Bursar','Cook'=>'Cook'),'label'=>'Position'));  echo $this->Form->submit('Save staff', array('class' => 'btn btn-success',  'title' => 'Click here to add the user') );   ?>  <?php echo $this->Form->end(); ?>  </div>

My Staff Model Staff.php like this

<?php      class Staff extends AppModel{          public $hasOne = array(              'Department'=>array(                  'className'=>'Department'                  ));          public $hasMany = array(              'Dpmember'=>array(                  'className'=>'Dpmember',                  'foreign_key'=>'Dpmember.staff_id'                  ),              'Subject'=>array(                  'className'=>'Subject',                  'foreign_key'=>'Subject.staff_id'                  ),              'Position'=>array(                  'className'=>'Position',                  'foreign_key'=>'Position.staff_id'                  )              );            }  ?>

In my StaffsController.php I have a function newstaff() with the code below

public function newstaff() {      /*Create a select form field for departments */      $department = $this->Staff->Department->find('list',array('fields'=>array('Department.title','Department.title')));      $this->set('department', $department);      /*End creation of a select form field for departments */        if (!empty($this->request->data)) {          debug($this->request->data); // returns all data          debug($this->Staff->Subject->subject); // has returned null          debug($this->Staff->Position->position); // has returned null          debug($this->Staff->Dpmember->departement); // has returned null          }  }

I don't know why but for some reason, I have not been able to found out. Running debug($this->request->data) returns expected data.

But accessing individual associated form fields returns null values not the expected data. Please help me.

Thank you

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: