3 CakePHP data levels

0

Let's say I have the following tabaelas

products

tags

manufacturers

So I have 3 models. One for each table. Each model is configured with the following scheme:

Product Model - > belongsTo: trademarks Branding model - > belongsTo: manufacturers

Until then I just modeled the data issue.

Now if I make the following command in any controller, assuming my model of the product tables is called ProductModel and my BrandModel tag

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

I will receive something like:

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

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

Is there any way for this result array to be 3 levels, ie the Manufacturers model come together within the Brands array?

  

Something that is NATIVE of CakePHP, without having to do two queries and   merge them

    
asked by anonymous 06.02.2015 / 20:44

1 answer

0

You can use Containable for this. 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 for this class, you get a similar result with the recursive attribute % of the Model , but Containable is recommended when you have complex joins. I prefer this way, as I make a more "personalized" query.

And after using, you can give unload :

$this->Produto->Behaviors->unload('Containable');
    
06.02.2015 / 22:18