Insert into a different model table in CakePHP 2.x

1

I'm making a site with a buddy cart, with CakePHP 2.x .

I created a statistics table, I made the model of it all right but I'll insert it into it, only when I go into the detail of some product.

When I enter the product detail I want to insert the product id and the category id to which it belongs, along with the date.

>

I was able to get him to insert the line with the saveAll() into the model, but he does not get the id from either.

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

Why did I pass an array like this to saveAll() .

array(Estatisticas.produto_id = $produto['id'], Estatisticas.categoria_id = $produto['Categoria']['id]);
    
asked by anonymous 08.08.2015 / 21:25

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 try too, to check the values, is a debug before create (), like this:

debug($salvarEstatisticas);

You see if the result is as expected.

    
14.08.2015 / 20:55