CAKEPHP 3:传递给 Cake\ORM\Table::patchEntity() 的参数 1 必须实现接口 Cake\Datasource\EntityInterface,给定的布尔值
CAKEPHP 3: Argument 1 passed to Cake\ORM\Table::patchEntity() must implement interface Cake\Datasource\EntityInterface, boolean given
感谢您的支持
我正在做一个调查系统的项目,每个问卷都有多个问题,另外还有添加问题我正在使用 Jquery 为所有要添加的问题加载多个字段集并同时保存而不是一个接一个地添加
但是我的代码有问题,因为它显示了这个错误
传递给 Cake\ORM\Table::patchEntity() 的参数 1 必须实现接口 Cake\Datasource\EntityInterface,给定布尔值,在 C:\wamp64\www\surveynaija\src\Controller\QuestionnairesController.[=45= 中调用] 第 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',
'published' => 1
],
];
但要实现这一点,您需要使用
$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">
希望有一天这对某人有用
感谢您的支持
我正在做一个调查系统的项目,每个问卷都有多个问题,另外还有添加问题我正在使用 Jquery 为所有要添加的问题加载多个字段集并同时保存而不是一个接一个地添加
但是我的代码有问题,因为它显示了这个错误
传递给 Cake\ORM\Table::patchEntity() 的参数 1 必须实现接口 Cake\Datasource\EntityInterface,给定布尔值,在 C:\wamp64\www\surveynaija\src\Controller\QuestionnairesController.[=45= 中调用] 第 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',
'published' => 1
],
];
但要实现这一点,您需要使用
$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">
希望有一天这对某人有用