fabian enos
fabian enos

Reputation: 71

How to specify range in find() cakephp

I’m new to CakePHP and I’ve tried searching but I can’t find an answer to this question.

To put it simply, I want the query to be something like this:

SELECT id from posts WHERE id IN (15,18,20);

But I don’t know what to put in the find() call.

Upvotes: 1

Views: 665

Answers (4)

keithhatfield
keithhatfield

Reputation: 3273

From the model it would be something like:

$ids   = array(15, 18, 20);
$posts = $this->find('all', array(
    'conditions'  => array(
        'Post.id' => $ids
     )
);

in the conditions array, you can pass an array of values to be used in the 'IN' clause

Upvotes: 1

Martin Bean
Martin Bean

Reputation: 39429

This is covered in the CakePHP online manual at http://book.cakephp.org/2.0/en/models/retrieving-your-data.html#complex-find-conditions. Simply specify an array in your conditions:

<?php
$ids = array(1,2,3,4,5,6);
$results = $this->Post->find('all', array(
    'conditions' => array(
        'Post.id' => $ids
    )
));

Upvotes: 2

Nikhil Agrawal
Nikhil Agrawal

Reputation: 285

$conditions = array("Post.title" => array("First post", "Second post", "Third post")) $this->find(all,array($conditions));

Chk it if it works.

Upvotes: -1

kaklon
kaklon

Reputation: 2432

From within the posts controller

$id_array = array(15, 18, 20);
$this->Post->find('all', array('Post.id' => $id_array));

More on the subject http://book.cakephp.org/2.0/en/models/retrieving-your-data.html

Upvotes: -1

Related Questions