【问题标题】:CakePHP why is associated null?CakePHP 为什么关联为空?
【发布时间】:2014-04-12 08:04:34
【问题描述】:

我有几个表和对应的模型,即Staffs、DpmembersSubjectsPositions表。 在我的 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-&gt;request-&gt;data) 返回预期数据。

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

谢谢

【问题讨论】:

  • 为什么你会认为$this-&gt;Staff-&gt;Subject-&gt;subject 包含任何内容? Cake2 不是 Cake3。
  • 你使用的是哪个 CakePHP 版本?
  • 我使用的是 cakePHP 2.3 版本

标签: php cakephp associations


【解决方案1】:

您似乎使用的是 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'];

【讨论】:

  • 哦,真的,我一直在到处研究,似乎找到了答案。我得到了 cakePHP 3.0 的答案?让我测试一下然后回来。
  • 嗯,我想我终于得到了价值观。哪些不为空。因为我创建了关系或者更确切地说是关联,并且使用的是 CakePHP 2.3 的语法。是 $this->request->data['associateModel']['FormField'];因此我应该这样做。 $this->request['Subject']['subject']; $this->request['Subject']['position']; $this->request['Subject']['department'];非常感谢@Oldskool
【解决方案2】:

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

$this-&gt;request-&gt;data['associateModel']['FormField'];

所以我应该这样做。

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

非常感谢@Oldskool

【讨论】:

    猜你喜欢
    • 2014-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多