Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CakePHP - Is it possible to use Containable behaviour with the read() method?

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?

like image 490
Gustavo Avatar asked Jun 01 '11 18:06

Gustavo


2 Answers

You can set also before the read() call:

$this->T->id = $id;
$this->T->contain(array(...));
$this->T->read();
like image 143
Carlos Gant Avatar answered Nov 03 '22 02:11

Carlos Gant


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'
            )
        )
    )
);
like image 32
JJJ Avatar answered Nov 03 '22 02:11

JJJ