1

Model Associations:

  • InfoFaturamento belongs to Contrato
  • Contrato has many ContratoCliente
  • ContratoCliente has many VwCliente

I want to fetch all VwClientes where InfoFaturamento equals an id passed by parameter. Is possible to do this without using joins option?

This is the code I tried. It's returning all the ContratoCliente:

$test = $this->InfoFaturamento->Contrato->ContratoCliente->find('all', [
    'contain' => [
        'Contrato' => [
            'InfoFaturamento' => [
                'conditions' => [
                    'InfoFaturamento.id' => $idInfoFaturamento
                ]
            ]
        ] 
    ],
]);
debug($test); die();

All the associations are set in the models.

I know I can do this with recursive option, but I don't like to use this because it returns lots of unnecessary data and it's slow. I could use joins too, but use joins is a good practice?

Keoma Borges
  • 683
  • 2
  • 12
  • 27

1 Answers1

1

Using containable behavior for such scenarios is indeed hectic and retrieves a lot of unnecessary data. You may have to unbind and unset the unwanted ones.

As an alternative, I'd suggest you to use joins. It's a lot faster and way more flexible, especially for situations like the one you've mentioned.

You could refer to these links:

CakePHP CookBook: Associations and joins

CakePHP: Joining multiple tables

Hope this helps.

Peace! xD

Community
  • 1
  • 1
Indrasis Datta
  • 8,692
  • 2
  • 14
  • 32
  • I thought it was the opposite, use Containable would be the best practice for these scenarions. Thank you. I'll use joins. – Keoma Borges Mar 22 '16 at 16:59