3 levels of Cakephp data

Asked

Viewed 86 times

0

Suppose I have the following tabaelas

products

brands

manufacturers

Then I will have 3 models. One for each table. Each model is configured with the following schema:

Product model -> belongsTo: trademarks Brand model -> belongsTo: manufacturers

So far I have only modelled the data issue.

Now if I do the following command in any controller, assuming that my model of the product tables is called Productomodel and my brand Marcamodel

$this->Produto->find('first')

I’m gonna get something like:

array(
    [Product] => array(
        // dados dos produtos
        [Brand] => array(
            // dados das marcas
        )
    )
)

That is, did not come the manufacturer data, even though it belongs to the model Brand.

There is some way that this result array is 3 levels, that is, the Manufacturers model comes together within the Tags array?

Something that is NATIVE to Cakephp, without having to make two queries and blend them

1 answer

0


You can use Containable for that. It would look like this:

$this->Produto->Behaviors->load('Containable');
$this->Produto->contain('Marca' => array('Fabricante'));
$produto = $this->Produto->find('first');

As indicated in the documentation of this class, you get a similar result with the attribute recursive of Model, but the Containable is recommended when you have complex associations. I prefer this way as I make a query more "customized".

And after using, you can give Unload:

$this->Produto->Behaviors->unload('Containable');
  • I will test here quickly and after doing this point and even leave your answer as best

Browser other questions tagged

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