cakephp选择下拉列表

时间:2014-11-05 22:19:52

标签: forms cakephp

我有以下型号:学生表格 FormsStream。

学生模型对两个模型都有外键。 学生。 form_id 表单表的 foreignKey 学生。 stream_id 对于 FormsStream 表来说是陌生的。我已经有表格和formsStreams的条目。

添加学生表单和流我从相应的下拉列表中选择两者。我已经创建了下拉列表。

我的问题

之前我能够保存/编辑学生表单,但在使这些字段选择外键后我无法保存新内容或编辑这些字段。我使用 cakePHP 2.5.4 。 但是我可以在SQL中做到这一点。 我似乎无法弄清楚选择列表的问题。

从数据库中获取表单的方法

public function loadStudentForm() {
    $this->loadModel('Student'); //load the associated models
    $this->loadModel('Form');
    $forms = $this->Form->find('list',array('fields' => 'form_name')); //get the forms from the database
    $this->set('forms',$forms);
}

从数据库获取流的方法

public function loadStreams() {
    $this->loadModel('FormsStream');
    $streams = $this->FormsStream->find('list',array('fields' => 'stream_name'));
    $this->set('streams',$streams);
}

学生添加方法

public function addStudent() {
    if ($this->request->is('post')) {
        $this->loadStudentForm();
        $this->loadStreams();
        $this->Student->create();
        if ($this->Student->save($this->request->data)) {
            $this->Session->setFlash(__('New student record added to the database.'));
            return $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The student could not be added.Please ensure all fields are correctly entered'));
        }
    }
}

add.ctp中的摘录,用于选择学生表单和流

echo $this->Form->input('forms',array('label' => 'Form')); 
echo $this->Form->input('streams',array('label' => 'Class stream'));

到目前为止,validateErrors的打印内容显示了

array(
    'Student' => array(
        'form_id' => array(
            (int) 0 => 'You must select the form'
        ),
        'stream_id' => array(
            (int) 0 => 'You must choose a stream'
        )
    ),
    'Form' => array(),
    'FormsStream' => array()
)

我甚至试图拦截burpsuite中的请求,我可以看到表单和流ID被传递。只是为什么它们在上面的数组中是空的

_method=POST&data%5BStudent%5D%5Badmission_no%5D=1002&data%5BStudent%5D%5Bfirst_name%5D=peter&data%5BStudent%5D%5Bmiddle_name%5D=per&data%5BStudent%5D%5Blast_name%5D=pee&data%5BStudent%5D%5Bgender%5D=Male&data%5BStudent%5D%5Bdate_of_birth%5D%5Bmonth%5D=11&data%5BStudent%5D%5Bdate_of_birth%5D%5Bday%5D=05&data%5BStudent%5D%5Bdate_of_birth%5D%5Byear%5D=2014&data%5BStudent%5D%5Bjoin_date%5D%5Bmonth%5D=11&data%5BStudent%5D%5Bjoin_date%5D%5Bday%5D=05&data%5BStudent%5D%5Bjoin_date%5D%5Byear%5D=2014&data%5BStudent%5D%5Bforms%5D=1&data%5BStudent%5D%5Bsstrong texttreams%5D=1

1 个答案:

答案 0 :(得分:1)

输入必须是form_id和stream_id。即使你用复数形式设置它们,cakephp也会用以下形式识别它。

echo $this->Form->input('form_id',array('label' => 'Form')); 
echo $this->Form->input('stream_id',array('label' => 'Class stream'));