Debug save () returns false CakePHP 3.0

I have a code that broke somewhere along the way, and I had trouble debugging it.

This is a simplified version.

$data = $this->request->data; $form = $this->Forms->get($data['id'], [ 'contain' => ['FieldsForms' => ['data'] ] ]); $form = $this->Forms->patchEntity($form, $data, ['associated' => [ 'FieldsForms.Data', ] ]); if ($this->Forms->save($form)) { // sunshine and rainbows } else { // wailing and gnashing of teeth } 

I leave the weeping and gnashing of teeth without any errors, as far as I can see if I am debugging the $ data, how it looks normal (although since it is quite long and contains a bunch of UUIDs, maybe I am missing something).

Verification errors are empty.

Saving returns false - any suggestions on how to debug this can save what I have left.

Thanks!

+6
source share
4 answers

The problem turned out to be for the data, as expected, but could not immediately see, because the save was returning false, and the data was quite large.

First, I made a subset of the problem data that displayed the same behavior, then, following the ndm suggestion, I changed the ORM / Table.php code for the save function as follows to see where the problem is:

 $x = $entity->errors(); if ($x) { debug($entity); debug($x); // if ($entity->errors()) { return false; } 

So that I can see what is happening and continue to correct the data.

+15
source

Not sure if the earlier answer is based on an older version, but in the latest cakephp (3.4) version you can get errors directly from the $ entity in the controller. The error array contains each failed field of the entity, with a child array of failed checks.

 <?php // In Articles Controller ... public function add(){ ... if ($this->Articles->save($article)) { $this->Flash->success(__('The Article has been saved.')); return $this->redirect(['action' => 'index']); } else { Log::Debug($article->errors()); } 
+1
source

Instead, change the core Cake code you can do:

 if ($this->Forms->save($form)) { // sunshine and rainbows } else { //you know now what fail $andTheErrorsAre = $entity->getErrors(); } 
0
source

For CakePHP 3.4. *

Try this code:

 if (!$this->Forms->save($form)) { $this->log($form->getErrors(),'error'); } 

Read more about magazines here: https://book.cakephp.org/3.0/en/core-libraries/logging.html#using-the-filelog-adapter.

0
source

Source: https://habr.com/ru/post/982790/


All Articles