Insert into a different model table in Cakephp 2.x

Asked

Viewed 137 times

1

I’m making a website with budget cart, with the CakePHP 2.x.

I created a statistical table, I made the model of it all right but I will do an insertion in it, only when I enter the detail of some product.

When I get into the product detail I want to insert into it the product id and the id of the category to which it belongs, together with the date.

I managed to get him to insert in the model the line with the saveAll() but he doesn’t take the id of either.

I would have to create an array somehow to receive this information?

Why I passed such an array on saveAll().

array(Estatisticas.produto_id = $produto['id'], Estatisticas.categoria_id = $produto['Categoria']['id]);

1 answer

1

Hello!

Try something like this, in the product controller, enter the attribute:

public $uses = array(
    'Estatisticas',
    ...
);

In your product detail action, do the following:

// array com os dados
$salvarEstatisticas = array(
    'produto_id' => $produto['id'],
    'categoria_id' => $produto['Categoria']['id']
);

// Informa que vai ser um novo registro
$this->Estatisticas->create();

// Insere os registros na tabela
$this->Estatisticas->save($salvarEstatisticas);

What you can also try, to check the values, is a debug before create(), like this:

debug($salvarEstatisticas);

you see if the result is as expected.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.