jose mey
jose mey

Reputation: 81

cakephp limit results find('all')

I just want to show on index.ctp all results from a model wich are true as conditions. Like this select:

select * from revistas where isonline = 'true';

Ive tried this code below on controller:

public function index() {
        //$iscond = $this->Revista->findAllByNome('testa');
        //$this->Revista->query("SELECT id, nome, acronimo, modified, user_id from xconv.revistas where isonline = 't'");
        $this->Revista->find('all', array(
            'contain' => array( 
                'conditions' => array('Revista.isonline' => 't'))));
        $this->Revista->recursive = 0;
        $this->set('revistas', $this->paginate());
    }

Those 2 comments above tried b4. It doesn return any errors, but dont do the condition.

Is there any other file of the MVC to write more code ? What went wrong ??

Ty

Upvotes: 1

Views: 1353

Answers (3)

Dave
Dave

Reputation: 29121

TLDR:

You need to supply your conditions to the actual pagination as opposed to doing a find() query, then doing a completely separate $this->paginate().

Explanation:

$this->paginate(); will actually run a query for you. So - basically, in your question, you're running a query for no reason, then doing paginate() which runs another query that has no conditions.

Instead, apply the condition like this:

$this->Paginator->settings = array('conditions'=>array('Revista.isonline' => 't'));
$this->paginate();

Note: As noted by your comment and another answer, there are multiple ways to apply conditions to paginate - the main point to this answer is pointing out that you need to apply the conditions to the paginate, not a previous query.

Upvotes: 3

Peter Krauss
Peter Krauss

Reputation: 13920

Using your clues at @Dave's answer comments, I think this is a solution with the "paginator filter",

public function index() {
    $this->Revista->recursive = 0;  //your old code
    $this->paginate['conditions']=array('Revista.isonline' => 't'); //new code
    $this->set('revistas', $this->paginate());  //your old code
}

Upvotes: 0

Alessandro Minoccheri
Alessandro Minoccheri

Reputation: 35963

try this:

public function index() {

        $this->Revista->find('all', array(
                'conditions' => array('Revista.isonline' => 't')));
        $this->Revista->recursive = 0;
        $this->set('revistas', $this->paginate());
    }

Upvotes: -1

Related Questions