CakePHP为什么关联为null


CakePHP why is associated null?

我有几个表和相应的模型,也就是说,员工、设备员科目职位表。在我的Staff模型中,我在Department上创建hasOne,因为我想从正在工作的Department表中检索数据。

但我也在DpmemberSubjectPosition模型上创建了更多的hasMany关联,因为我想保存相应的员工记录。

视图newstaff.ctp看起来像这个

<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>

我的员工模型Staff.php像这个

<?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'
                )
            );      
    }
?>

在我的StaffsController.php中我有一个函数newstaff(),代码在下面

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
        }
}

我不知道为什么,但由于某种原因,我一直没能找到答案。运行debug($this->request->data)将返回预期的数据。

但是访问单独的关联表单字段会返回null值,而不是预期的数据。请帮帮我。

感谢

您似乎在使用CakePHP 3.0语法,而使用的是CakePHP 2.3。返回的数据是Cake 2中的一个数组,而不是一个对象。所以数据在数组键下,比如:

$this->request->data['Staff']['Subject']['subject'];
$this->request->data['Staff']['Position']['position'];
$this->request->data['Staff']['Dpmember']['departement'];

我想我终于得到了不为null的值。由于我在模型之间创建了关系或更确切地说是关联,并且正在使用CakePHP 2.3,因此正确的方法是

$this->request->data['associateModel']['FormField'];

所以我应该这么做。

$this->request['Subject']['subject']; $this->request['Subject']['position']; $this->request['Subject']['department'];

非常感谢@Oldskool