5

I'm new with CakePHP and can't figure out how to call the read() method of a model class with a Containable behaviour. I can do the following with find()

$this->T->find('all', array (
    'contain' => array (
        'C', 
        'L' => array (
            'fields' => array ('L.id, L.time'),
            'I' => array (
                'fields' => array ('I.id','I.time'),
                'J.name',
                'J.id'
            )
        )
    )
);

That works just as I expected, but I don't want to get all instances of T, but instead the one with id = $id so I can pass it to the 'view' View. but the array passed to find doesn't work when doing

$this->T->read(
array (
        'contain' => array (
            'C', 
            'L' => array (
                'fields' => array ('L.id, L.time'),
                'I' => array (
                    'fields' => array ('I.id','I.time'),
                    'J.name',
                    'J.id'
                )
            )
        )
, $id)

Is there any way to do this?

Gustavo
  • 195
  • 2
  • 11

2 Answers2

16

You can set also before the read() call:

$this->T->id = $id;
$this->T->contain(array(...));
$this->T->read();
Carlos Gant
  • 731
  • 6
  • 15
  • 1
    As find doesn't set the Model->data variable the effect of using find( first ) and read( ) are not the same. If all he wanted was data to move to the view the find( first ) solution is fine but like Dunhamzzz said - the question either needs corrected for clarity / relationship to the answer or the answer is wrong. If *I* really needed to use containable with model::read and find( first ) wasn't a good solution then this would be a worthless search result. – Abba Bryant Jun 02 '11 at 20:36
  • To paraphrase my above comment - THIS ANSWER IS THE CORRECT ANSWER TO THE QUESTION AS THE AUTHOR PHRASED IT – Abba Bryant Jun 02 '11 at 20:47
  • Yes, all I wanted was the data to be moved to a view. I'm sorry I didn't express the question correctly. It's fixed now, thanks, Carlos – Gustavo Jun 03 '11 at 04:18
3

I believe you can set the behavior so that it applies to read(), but you can also use find( 'first' ) if the data is all you need:

$this->T->find( 'first', array (
    'conditions' => array(
        'T.id' => $id
    ),
    'contain' => array (
        'C', 
        'L' => array (
            'fields' => array ('L.id, L.time'),
            'I' => array (
                'fields' => array ('I.id','I.time'),
                'J.name',
                'J.id'
            )
        )
    )
);
JJJ
  • 32,902
  • 20
  • 89
  • 102
  • 1
    I'm confused to why you've accepted this, the question is "..use containable behaviour with the read() method" - this is not using the read() method. – Dunhamzzz Jun 02 '11 at 12:36
  • @Gustavo: Rephrase the question to explain that you simply need the data, not specifically from a model::read call. – Abba Bryant Jun 02 '11 at 20:37