问题描述
感谢您的支持
我正在为一个调查系统设计一个项目,每个调查表都包含多个问题,而另外一些添加问题,我正在使用Jquery加载要同时添加并保存的所有问题的多个字段集而不是一个接一个添加
传递给Cake \ ORM \ Table :: patchEntity()的参数1必须实现Cake \ Datasource \ EntityInterface接口,给定布尔值,在C:\ wamp64 \ www \ surveynaija \ src \ Controller \ QuestionnairesController.PHP中调用在第65行
第65行指向$result = $questions->patchEntity($result,$this->request->getData());
这是控制器代码
public function addquestions(){
$data[] = [
'question' => 'question','questionnaire_id' => 'questionnaire_id','questiontype_id' => 'questiontype_id'
] ;
$questions = TableRegistry::getTableLocator()->get('Questions');
$entities = $questions->newEntities($data);
$result = $questions->savemany($entities,array('deep'=>true,'validate'=>false,'atomic' => true));
//$question = $this->Questions->newEntity();
if ($this->request->is('post')) {
$result = $questions->patchEntity($result,$this->request->getData());
if ($result) {
$this->Flash->success(__('The question has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The question Could not be saved. Please,try again.'));
}
$questionnaires = $questions->Questionnaires->find('list',['limit' => 200]);
$questiontypes = $questions->Questiontypes->find('list',['limit' => 200]);
$answers = $questions->Answers->find('list',['limit' => 200]);
$this->set(compact('result','questionnaires','questiontypes','answers'));
echo "<pre>";
//print_r($data);
echo "</pre>";
//echo $data['question'];
}
这是我的查看代码
<div class="questions form large-9 medium-8 columns content " >
<?= $this->Form->create($result) ?>
<button class="add_field_button">Add More Fields</button>
<fieldset class="input_fields_wrap" id="addForm">
<legend><?= __('Add Question') ?></legend>
<?PHP
echo $this->Form->control('question');
echo $this->Form->control('questionnaire_id',['options' => $questionnaires]);
echo $this->Form->control('questiontype_id',['options' => $questiontypes]);
echo $this->Form->control('answers._ids',['options' => $answers]);
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
<?PHP
echo "<pre>";
var_dump($result);
echo "</pre>";
?>
这是我的jquery代码
<script type="text/javascript">
$(document).ready(function() {
var max_fields = 10; //maximum input Boxes allowed
var wrapper = $(".input_fields_wrap"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button ID
var addFormField = '<?PHP echo $this->Form->control("question"); ?>'
+ '<?PHP echo $this->Form->control("questionnaire_id",["options" => $questionnaires]); ?>'
+ '<?PHP echo $this->Form->control("questiontype_id",["options" => $questiontypes]); ?>'
+ '<?PHP echo $this->Form->control("answers._ids",["options" => $answers]);?>'
;
var x = 1; //initlal text Box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
if(x < max_fields){ //max input Box allowed
x++; //text Box increment
$(wrapper).append(addFormField); //add input Box
}
});
$(wrapper).on("click",".remove_field",function(e){ //user click on remove text
e.preventDefault(); $(this).parent('div').remove(); x--;
})
});
解决方法
在上面的问题中,问题是创建一个使用cakephp中的saveMany()函数的表单
但是这样做,您需要准备表单中的数据以使其成为数组
$data = [
[
'title' => 'First post','published' => 1
],[
'title' => 'Second post',];
但是要实现这一点,您需要使用
$this->request->getData('ModelName' ['field1','field2','field3' ])
然后按照食谱
https://book.cakephp.org/3/en/orm/saving-data.html#converting-multiple-records
在视图文件中,您可以使用for循环或jquery,但可以使用html输出
<input type="text" name="Modelname[1][field]" id="modelname-1-field">
<input type="text" name="Modelname[2][field]" id="modelname-2-field">
<input type="text" name="Modelname[3][field]" id="modelname-3-field">
希望有一天能为某人服务