cakephp选择下拉列表(cakephp select dropdown list)

编程入门 行业动态 更新时间:2024-10-13 12:22:33
cakephp选择下拉列表(cakephp select dropdown list)

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

学生模型有两个模型的外键。 学生。 form_id表单StudentforeignKey stream_idFormsStream表是异常的 。 我已经有表格和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

i have the following models: Student,Form,FormsStream.

the student model have foreign keys to both models. Student.form_id is a foreignKey to the form table,Student.stream_id is foreign to the FormsStream table. i already have entries for both forms and formsStreams.

to add the student Form and Stream i select both from respective drop-down list. i already created the drop-down list.

my Problem.

previously i was able to save/edit student form and stream but after making those fields foreign keys to be selected i cant save new or edit those fields. i use cakePHP 2.5.4. however i can do that in SQL. i cant seem to figure out the problem with the select lists.

my method to get the forms from the database

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

method to get streams from database

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

student add method

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

the extract from add.ctp for selecting student form and stream

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

so far printing contents of validateErrors shows this:

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

i've even tried to intercept the request in burpsuite and i can see the form and stream id are passed.just dont get why they are empty in the above array

_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

最满意答案

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

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

The input must be form_id and stream_id. Even if you set them with plural form cakephp recognize it in the following form.

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

更多推荐

本文发布于:2023-04-29 03:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1334474.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:列表   cakephp   select   list   dropdown

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!