PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, December 31, 2021

[FIXED] CAKEPHP 3: Argument 1 passed to Cake\ORM\Table::patchEntity() must implement interface Cake\Datasource\EntityInterface, boolean given

 December 31, 2021     cakephp-3.0, jquery, php     No comments   

Issue

Thank you for your support

I'm working on a project for a survey system with every questionnaire having multiple questions and in other to make adding questions i'm using Jquery to load multiple field set for all the question to be added and save at the same time Instead of adding it one after the other

but i'm having Issues with my code as it shows this error

Argument 1 passed to Cake\ORM\Table::patchEntity() must implement interface Cake\Datasource\EntityInterface, boolean given, called in C:\wamp64\www\surveynaija\src\Controller\QuestionnairesController.php on line 65

The line 65 refers to $result = $questions->patchEntity($result, $this->request->getData());

here is the controller code

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

here is my view code

<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>";
    
?>

and here is my jquery code

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

Solution

In the question above the problem is to create a form that use the saveMany() function in cakephp

but in doing so the you need to prepare the data from the form to be in array

 $data = [
        [
            'title' => 'First post',
            'published' => 1
        ],
        [
            'title' => 'Second post',
            'published' => 1
        ],
    ];

but to achieve this you need to use

$this->request->getData('ModelName' ['field1', 'field2', 'field3' ])

then following the cookbook

https://book.cakephp.org/3/en/orm/saving-data.html#converting-multiple-records

in the view file you can use a for loop or jquery but your html output

<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">

hope this serves someone someday



Answered By - mopo
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing