[CakePHP 3] Erreur lors du patch d'une entity

Par Romano83, il y a 10 ans


Bonjour à tous,
ça fait un petit moment que je n'ai pas touché à CakePHP et je me retrouve avec une erreur que je ne parviens pas à comprendre.

Au niveau d'un de mes controller, j'au une fonction add classique :

public function add($id = NULL) { $gallery = $this->Galleries->newEntity(); if ($this->request->is('post')) { $data = $this->request->data; (isset($id)) ? $gallery->id = $id : ''; $gallery = $this->Galleries->patchEntity($gallery, $data); if ($this->Galleries->save($gallery)) { $this->Flash->success(__('La gallerie a bien été sauvegardée')); return $this->redirect(['action' => 'index']); } else { $this->Flash->error(__('Une ou plusieurs erreurs se sont produites. Merci de les corriger avant de resoumettre le formulaire')); } } else { $gallery->id = $this->Galleries->getDraftId($this->Galleries, ['category_id' => 1]); } $categories = $this->Galleries->Categories->find('list', ['limit' => 200]); $this->set(compact('gallery', 'categories')); $this->set('_serialize', ['gallery']); }

Lorsque je souhaite sauvegarder l'entity, j'ai une erreur du type Maximum function nesting level of '100' reached, aborting!.

Après vérification au niveau de la fonction, je m'apercois d'une petite bizarrerie lorsque je fais l'appel à patchEntity.
En effet, lorsque je debug $gallery après le patchEntity, je me retrouve avec ça :

object(App\Model\Entity\Gallery) { 'id' => '2', 'category_id' => (int) 2, 'slug' => 'azeazeaz', 'title' => object(App\Model\Entity\Gallery) { 'id' => '2', 'category_id' => (int) 2, 'slug' => 'azeazeaz', 'title' => object(App\Model\Entity\Gallery) { 'id' => '2', 'category_id' => (int) 2, 'slug' => 'azeazeaz', 'title' => object(App\Model\Entity\Gallery) { 'id' => '2', 'category_id' => (int) 2, 'slug' => 'azeazeaz', 'title' => object(App\Model\Entity\Gallery) { 'id' => '2', 'category_id' => (int) 2, 'slug' => 'azeazeaz', 'title' => object(App\Model\Entity\Gallery) { 'id' => '2', 'category_id' => (int) 2, 'slug' => 'azeazeaz', 'title' => object(App\Model\Entity\Gallery) { 'id' => '2', 'category_id' => (int) 2, 'slug' => 'azeazeaz', 'title' => object(App\Model\Entity\Gallery) { 'id' => '2', 'category_id' => (int) 2, 'slug' => 'azeazeaz', 'title' => object(App\Model\Entity\Gallery) { 'id' => '2', 'category_id' => (int) 2, 'slug' => 'azeazeaz', 'title' => object(App\Model\Entity\Gallery) { 'id' => '2', 'category_id' => (int) 2, 'slug' => 'azeazeaz', 'title' => object(App\Model\Entity\Gallery) { // to be continue...

J'avoue ne pas comprendre pourquoi j'ai ça...

Une idée ?

Merci d'avance,
Romain

4 réponses

Ferias Quarante, il y a 10 ans

Bonjour,

Peux tu montrer le debug de $this->request->data et la table & entity Gallery ?

Merci

Romano83, il y a 10 ans

Salut,

voici le debug de $this->request->data :

[ 'category_id' => '2', 'title' => 'Gallerie 2', 'link' => 'https://youtu.be/9QIXzPGlAJs' ]

Pour ce qui est de la table et de l'entity, je n'y ai pas touché.
J'ai juste rajouté ceci au niveau de la classe Gallery(entity) :

protected function _setTitle($title) { $this->set('slug', \strtolower(Inflector::slug($title))); return $this; }
Ferias Quarante, il y a 10 ans

Le problème vient de ta méthode _setTitle, tu retourne $this.
Dans la documentation il faut retourner la valeur soit ici $title :

protected function _setTitle($title) { $this->set('slug', \strtolower(Inflector::slug($title))); return $title; }
Romano83, il y a 10 ans

Ah oui ! Bien vu !
Je n'avais pas vu cette erreur !
Merci pour ton aide ;)