2 years ago

#39713

test-img

Oliv

Save associations not working in cakephp 4

I have these nested associations :

CustomRubriques hasOne ExtendedRubriques hasMany Photos

Photos has fields id and legend

I'm trying to save legend from CustomRubriques like this :

// CustomRubriques/edit.php
echo $this->Form->control('extended_rubrique.photos.0.id');
echo $this->Form->control('extended_rubrique.photos.0.legend');
echo $this->Form->control('extended_rubrique.photos.1.id');
echo $this->Form->control('extended_rubrique.photos.1.legend');
// etc..
// CustomRubriquesController.php
public function edit($id)
{
    $rubrique = $this->CustomRubriques->findById($id)->firstOrFail();

    if ($this->request->is(['post', 'put'])) {
        
        $rubrique = $this->CustomRubriques->patchEntity($rubrique, $this->request->getData());

        debug($rubrique); // $rubrique->extended_rubrique->photos[0]['id'] and $rubrique->extended_rubrique->photos[0]['legend'] are well defined

        if ($this->CustomRubriques->save($rubrique, ['associated' => ['ExtendedRubriques', 'ExtendedRubriques.Photos']])) {
            return $this->redirect(['action' => 'view', $rubrique->id]);
        }
    }

    $this->set(compact('rubrique'));
}

The legend of the photos are not saved (without any error returned) and I really don't know why !

(Note that legend is true in Photo Entity's $_accessible )

Could anybody tell me what I'm doing wrong ?

cakephp

orm

associations

cakephp-4.x

0 Answers

Your Answer

Accepted video resources