0

everyone. I have a problem with patchEntities. I want to edit entities by using form.

  1. Edit a entities by using form (view)
  2. Send the data (view)
  3. apply data by using patchEntities (controller)

However, I get an error when I try to patchEntities.

Table Name: tempTable
column: id, name, age

Controller

$data = $this->table->find('all');
if($this->request->is(['post'])){
    $entities = $table->patchEntities($data->toArray(), $this->request->data());
}
$this->set(compact('data'));

View

<?= $this->Form->Create('tempTable');?>
<?php foreach ($data as $key=>$d): ?>
  <?= $this->Form->Control($key.'.name', ['type' => 'text', 'default' => $d->name]);?>
<?php endforeach; ?>
<?= $this->Form->submit('submit');?>
<?= $this->Form->end();?>

I got an error "Call to member function patchEntities() on boolean"
I can't understand why I get the error.
Could you give me a hint? Thank you very much.

1 Answers1

0

You didn't execute your query, so you can't use the toArray() method on $data, which is why $data->toArray() returns false (a boolean).

$this->table->find('all') only sets up the query. You'd need to add something like ->all() to execute it.

Try this piece of code instead :


$query = $data = $this->table->find('all');
$data = $query->all();

Félix
  • 388
  • 1
  • 9
  • 1
    Actually `toArray()` can be used with query objects, by default all methods of `\Cake\Datasource\ResultSetDecorator` can be used on queries (unless concrete methods with the same name exist on the query object). Calling one of those methods will automatically execute the query, and call the method on the generated result set. **https://book.cakephp.org/3.0/en/orm/query-builder.html#queries-are-collection-objects** – ndm Jul 24 '19 at 12:45
  • Interesting, thanks for telling! Any idea why @SumuraiSasuraino is having this error, then ? – Félix Jul 24 '19 at 13:06
  • It's because `$table` is a boolean. Howsoever the value for that variable is being obtained, it's not being done the right way. It's often this: **https://stackoverflow.com/questions/31813722/what-means-call-to-a-member-function-on-boolean-and-how-to-fix/31815095#31815095**, spelling mistakes or models not been loaded. – ndm Jul 24 '19 at 13:39
  • 1
    Thank you very much both of you! The error happened due to spelling. Thank you for giving your time to solve this error! – Sumurai Sasuraino Jul 24 '19 at 15:37