首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >CakePHP为什么关联为null?

CakePHP为什么关联为null?
EN

Stack Overflow用户
提问于 2014-04-12 08:04:34
回答 2查看 116关注 0票数 1

我有几个表和相应的模型,即Staffs、DpmembersSubjectPosition表。在我的Staff模型中,我在 Department 上创建了hasOne,因为我想从正在工作的Department表中检索数据。

但是,我还创建了更多的hasMany on Dp员SubjectPosition模型,因为我想保存相应的员工记录。

视图newstaff.ctp如下所示

代码语言:javascript
运行
复制
<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像这样

代码语言:javascript
运行
复制
<?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(),它的代码如下

代码语言:javascript
运行
复制
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)将返回预期的数据。

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

谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-04-12 11:19:28

您似乎在使用CakePHP 3.0语法,而使用CakePHP 2.3。返回的数据是Cake 2中的数组,而不是对象。因此,数据在数组键下面,例如:

代码语言:javascript
运行
复制
$this->request->data['Staff']['Subject']['subject'];
$this->request->data['Staff']['Position']['position'];
$this->request->data['Staff']['Dpmember']['departement'];
票数 1
EN

Stack Overflow用户

发布于 2014-04-12 12:55:21

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

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

所以我应该这么做。

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

非常感谢你@Oldskool

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23027879

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档