0

I have a problem with contain :( , let me explain:

Models Relations:

Client -> hasMany -> Card -> hasAndBelongsToMany -> Partner -> hasMany -> Contact

What i need is to get all partners and contacts of Client with codigo = 24150 and Contacts with coords_lat = 38.71093.

Code:

$conditions = array('conditions' => 
   array('codigo' => '24150')
);

$contain = array(
   'contain'=> array(
    'Partner' => array(
       'Contact' => array('conditions' => array('coords_lat' => '38.71093')) 
    )
   )
);

$parceiros = $this->Client->find('all', array($conditions, $contain));

I've added

var $actsAs = array('Containable'); 

to app_model.php

To be totally honest with you, i don't really understand what is happening here... truth is that mysql does huge queries and takes tons of time but i don't see it selecting by "codigo = 24150"

I would appreciate some help.

Thanks in advance.

Rui

Rui
  • 516
  • 3
  • 8
  • What is the output you're getting right now? What's the content of `$parceiros`? – vindia Apr 05 '11 at 11:19
  • I think you should check @Leo's answer. That's probably where you go wrong. Your syntax looks okay. – vindia Apr 05 '11 at 13:06
  • No output... because cakephp generates crazy queries, please check here: http://pastebin.me/24d7e33ce334e56087dcb657080325ce Im sorry for the name of the tables, they are in portuguese Cliente = Cliente, CgdCartoesCliente = Card, cgd_cartoes_clientes = HATBM connection table – Rui Apr 05 '11 at 13:11

3 Answers3

2

You could also keep both of your arrays $conditions and $contain separated..

and change the last line to be:

$parceiros = $this->Client->find('all', array_merge($conditions, $contain));

notice the array_merge function..
this should fix your problem without needing to use only one array for both of them

Mouad Debbar
  • 3,126
  • 2
  • 20
  • 20
1

You should be getting an error stating that Client is not related to Partner. Try the following

$this->Client->contain(
    'Card'=>array('Partner' => array(
       'Contact' => array('conditions' => array('coords_lat' => '38.71093')) 
    )
   )
)
);

$parceiros = $this->Client->find('all', array('conditions' => 
                                            array('Client.codigo' => '24150')
                                            )
                                  )
Leo
  • 1,521
  • 12
  • 18
  • No luck.... still generates long queries and mysql timesout :( also i don't see it selecting by codigo 24150. Please check the queries here: http://pastebin.me/24d7e33ce334e56087dcb657080325ce – Rui Apr 05 '11 at 13:13
  • OK Rui lets try a little process of elimination. Comment out the $this->Client->contain line. Then pr() the contents of $parceiros, what do you get? – Leo Apr 05 '11 at 13:28
  • Something is strange, apparently $conditions is ignored in the find :( I did add recursive = -1 and query generated is "select * from clientes WHERE 1 = 1" ? :( – Rui Apr 05 '11 at 13:43
  • So did it return all the Clients? execute just the last line again ( I have amended it to include the model name ) – Leo Apr 05 '11 at 13:59
  • It's getting better now.... Returned the Client and the Cards... but didn't return Partners and Contacts. (I did uncomment the contain line) – Rui Apr 05 '11 at 14:05
  • Right we're going round in circles now. Executing JUST the last line do you get the Client(s) returned with a 'codigo' of 24150 – Leo Apr 05 '11 at 14:16
  • Leo, yes.... and with cards too. It returns the Client with codigo 24150 and his cards (which are IDs 14,23,29)... – Rui Apr 05 '11 at 14:24
  • Ok if you're not getting anything with the contain uncommented you.may have an issue.with your relationships. Set recursive to 2 and what do you get? – Leo Apr 05 '11 at 15:24
  • I tried setting recursive to 5... and i get all the relationships fine... when i add contain, it stops working... also 'conditions' => array('coords_lat' => '38.71093') seems to be ignored. – Rui Apr 05 '11 at 15:31
  • tried to simplify contain to: $this->Client->contain = array( 'Card'=>array('Partner')); but doesn't work too :( – Rui Apr 05 '11 at 15:40
  • No worries, I stupidldy was assigning an array to $this->client->contain which is obviously a method. I have re-edited my code – Leo Apr 06 '11 at 09:38
1

Finally fixed it, cakephp didn't like having conditions and contain in different arrays. The solution for this is:

$conditions = array(
    'conditions' => array('codigo' => '24150'),
    'contain' => array(
        'Card'=>array(
            'Partner' => array (
                'Contact' => array(
                    'conditions' => array('Contact.coords_lat' => '38.710930')
                 )
             )
         )
     )
 );

 $parceiros = $this->Client->find('all', $conditions);
Rui
  • 516
  • 3
  • 8