0
votes

In cake PHP, I use containable behavior to join a bunch of tables and make a nice, large array with a bunch of data, like so:

$this->paginate = array(
    'contain' => array(
        'House' => array(
            'Room' => array(
                'Chair' => array(),
            ),
        ),
        'Apartment' => array(),
    ), 
    "limit" => $limit, 
    'conditions' => array('Dwelling.id' => 1), 
);

However, this of course returns every column of every table, and what I really want is just the Chair columns, so I do this, adding a "fields" array to a lot of the models:

 $this->paginate = array(
    'contain' => array(
        'House' => array(
            'fields' => array("id"),
            'Room' => array(
                'fields' => array("id"),
                'Chair' => array(),
            ),
        ),
        'Apartment' => array(),
    ), 
    "limit" => $limit, 
    'conditions' => array('Dwelling.id' => 1), 
    'fields' => array("id"),
);

And then CakePHP says "screw it, I'm just not going to return anything at all besides the Dwelling model." Why would it do this? Upon looking at the actual MYSQL queries, it looks to be querying the database properly for all this; it just doesn't package it all into the resulting array...

1
Ran into another problem without an answer while using Containable behavior. I'm probably just going to say screw it and go with my custom-made SQL query generator. Cakephp blows for doing anything outside the box.Nate L

1 Answers

1
votes

If you only want "Chair" then why are you adding "Apartment"? Anyways, try this

 $this->paginate = array(
'contain' => array(
    'House' => array(
        'fields' => array("id"),
        'Room' => array(
            'Chair' => array(),
        ),
    ),
    'Apartment' => array(),
), 
"limit" => $limit, 
'conditions' => array('Dwelling.id' => 1), 
'fields' => array("House.id", "Chair.id", "Chair.whatever"),

);