Problème Sauvegarde de données ManyToMany

Par Arbi, il y a 6 ans


Bonjour,

Voila je rencontre un petit problème avec mon code.

Ce que je fais

public function add() { $post = $this->Posts->newEmptyEntity(); $categories = TableRegistry::getTableLocator()->get('Categories')->find('list')->toArray(); $technologies = TableRegistry::getTableLocator()->get('Technologies')->find('list')->toArray(); if ($this->request->is('post')){ $data = $this->request->getData(); $data['tutoriel_id'] = 0; $post = $this->Posts->patchEntity($post,$data,[ 'associated' => ['Technologies'] ]); $this->Posts->save($post); return $this->redirect(['action'=>'index']); } $this->set(compact('categories','technologies')); }

voici l'assocation dans le Model Post

$this->belongsToMany('Categories', [ 'joinTable' => 'posts_categories', 'foreignKey' => 'post_id', 'targetForeignKey' => 'categorie_id', ]);

voici l'assocation dans le Model Categories

$this->belongsToMany('Posts', [ 'joinTable' => 'posts_categories', 'foreignKey' => 'category_id', 'targetForeignKey' => 'post_id', ]);

Merci d'avance

Aucune réponse