1
votes

Since a long time as per my knowledge I was using recursive to control my model relationship. If I make any relation between my models it would surely be autoconnected with paginate. To control that I need to use recursive. By default its value is 1 and to contro; that I have to use it as -1 or 0. Yes I read about Containable behaviour that how it automatically control fetching result from other Models Though relationships are made.

I went through same as writing

public $actsAs = array('Containable'); 

In my controller I wrote

$this->Album->Behaviors->load('Containable', array('autoFields' => false, 'recursive'=>false));

But then also my default paginate called data from other Model as well as fetch queries with other Models.

$this->paginate['Album'] = array('conditions' => $condition, 'limit' => '50', 'order' => array('Album.id' => 'DESC'));
$this->set('albums', $this->paginate('Album'));

My default pagination code as per my expectation data would be only from Album Model and to get from other Model I have to describe it in Pagination but when I checked it in Debug Kit it shows this.

enter image description here

As well as fetch data from all variables. enter image description here

What should I do ?? Where I am wrong ??

2
Can you re-write your question please. I’m having trouble understanding your problem and what outcome you’re expecting. - Martin Bean
I mean to say If I am writing public $actsAs = array('Containable'); in my Model then associated Models data shouldn't be fetched. If I want data from associated model then I should mention in controller. But by default its fetching associated model data no matter I have written public $actsAs = array('Containable'); - Sankalp
@Martin Bean I am expecting response from you if you got my doubt. - Sankalp

2 Answers

1
votes

You don’t want the data of related models, correct me if I’m wrong. For this you need to set contain property to false. This will only bring the data of Album model

$this->paginate['Album'] = array('conditions' => $condition,'contain' => false 'limit' => '50', 'order' => array('Album.id' => 'DESC'));
$this->set('albums', $this->paginate('Album'));

Contain will be helpful when you want to attach multiple model with your query like

$this->paginate['Album'] = array('conditions' => $condition,'contain' => array('model1','model2'), 'limit' => '50', 'order' => array('Album.id' => 'DESC'));

I hope this will work for you. Thanks

0
votes

Even if your model is set to be containable, you can still set the recursive to false to prevent the fetching of associated data.

Sample model:

<?php
class Article extends AppModel {

    public $actsAs = array('Containable');
    public $belongsTo = array('Category');
}

Sample controller:

<?php
class ArticlesController extends AppController {

    public function index() {
        $this->Article->recursive = 0;
        $this->set('articles', $this->paginate('Article'));
    }

    public function view($id) {
        $this->Article->recursive = 0;
        $this->set('article', $this->Article->findById($id));
    }
}