Save entities related to cakephp-3

-1

I do not understand why related entities are not working with cakephp, for example: I have an entity company, and this company has a user. In my view, the company values correctly put [$ company-> cnpj] and the values for user, I put through the relationship [$ company-> user-> name], but when saving the data, they are not persisted. Follow my controller.

public function add()
{
    $empresa = $this->Empresas->newEntity();
    $tableUser = TableRegistry::get("Users");
    $user = $tableUser->newEntity();
    $empresa->user = $user;
    if ($this->request->is('post')) {
        $empresa = $this->Empresas->patchEntity($empresa, $this->request->data, ['associated' => ['Users.User']]);
        if ($this->Empresas->save($empresa,['associated' => ['User']])) {
            $this->Flash->success(__('Sua empresa foi cadastrada com sucesso!'));
            return $this->redirect(['controller' => 'Public', 'action' => 'boasvindas']);
        } else {
            $this->Flash->error(__("Ohh não! Houve um problema, por favor comunique-nos o quanto antes."));
        }
    }
    $users = $this->Empresas->Users->find('list', ['limit' => 200]);
    $this->set(compact('empresa', 'users'));
    $this->set('_serialize', ['empresa']);
}

I do not know the $ this-> Business-> save ($ company, ['associated' => '' User ']]) is wrong. I would like some help.

    
asked by anonymous 01.09.2016 / 01:42

1 answer

1

The ['associated' => ['User']] must be small.

Try this:

$empresa = $this->Empresas->patchEntity($empresa, $this->request->data, ['associated' => ['user']]);

Remembering that in the form the name of the associated fields must also be small:

echo $this->Form->input('user.0.id');
echo $this->Form->input('user.0.name');
echo $this->Form->input('user.1.id');
echo $this->Form->input('user.1.name');
    
12.09.2016 / 22:16