How to remove table from the results of a find () of CakePHP?

2

I have 3 related tables: Usuario , Movimentacao and Grupo

I'm using:

$usuarios = $this->Usuario->find('all',array(
    'recursive' => 1,
    'fields' => "Usuario.id, Usuario.nome, Usuario.email"
    'order' => array('Usuario.nome ASC')
));

However, recursive => 1 is bringing all three tables together. If it is 0, it returns only User. I need to return only User and Group, because, bringing Movement, the query is too slow. How do I remove Movimetacao from the query?

    
asked by anonymous 04.02.2014 / 18:48

2 answers

1

You can temporarily disassociate the other model:

$this->Usuario->unbindModel(array('hasAndBelongsToMany' => array('Movimentacao')));

(Replace hasAndBelongsToMany with the type of relationship you are using.)

    
04.02.2014 / 19:05
0

When giving unbind , you completely remove the relationship of this model during execution.

I recommend doing the data selection in 2 ways:

For direct associations, you can pass the recursive in conjunction with the fields and thereby limiting the returned data. For example:

$result = $this->Usuario->find('all', array(
    'conditions' => array('Usuario.status' => 'ativo'),
    'recursive' => 1,
    'fields' => array('Usuario.nome', 'Grupo.nome')
));

For deep associations use Containable Behavior ( link )

Example of use in a controller:

$this->Usuario->Behaviors->load('Containable');
$result = $this->Usuario->find('all', array(
    'conditions' => array('Usuario.status' => 'ativo'),
    'contain' => array('Grupo')
));
    
05.02.2014 / 15:10