开发者

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?


You can set also before the read() call:

$this->T->id = $id;
$this->T->contain(array(...));
$this->T->read();


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'
            )
        )
    )
);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜