George C.
George C.

Reputation: 3

CakePHP - select from database with condition

I have this selector:

$this->Table->find('list',array('contain'=>false,'conditions'=>array('status'=>1,'unit_id'=>null,'country_id'=>$countryId,'eday'=>$eday),'fields'=>'id'));

And this works perfect. But now i need to have another one i cant find how to do this ;)

I need to select all records from Table but with condition:

'eday'>=$eday AND 'eday'<$eday+7 

Its this posibble in easy way? Maybe this is stupid question but i dont have exp in PHP ;)

Upvotes: 0

Views: 1512

Answers (3)

rahim.nagori
rahim.nagori

Reputation: 666

Have you tried something like this :

$conditions['status'] = 1;
$conditions['unit_id'] = null;
$conditions['country_id'] = $countryId;
$conditions['eday >='] = $eday;
$conditions['eday <'] = $eday + 7;

$this->Table
    ->find('list') //either use this
    ->find('all') //or use this
    ->find() //or this
    ->where($conditions)
    ->select(['addFiledsToSelectHere'])

This looks more cleaner.

Upvotes: 0

Beginner
Beginner

Reputation: 4153

In cakephp the equivalent for and condition is
Example: column1 = 'value' AND column2 = 'value'

'AND' => array(
    'column1' => 'value',
    'column2' => 'value'
)

So your query builder would be like this

$this->Table->find('list',array(
    'contain' => false,
    'conditions' => array(
        'status' => 1,
        'unit_id' => null,
        'country_id' => $country,
        'AND' => array(
            'eday >=' => $eday,
            'eday <' => ($eday+7)
        )
    ),
    'fields'=>'id'
));

Upvotes: 1

Ravi Roshan
Ravi Roshan

Reputation: 570

Just pass an AND array with in your current conditions array :

$this->Table->find('list',array('contain'=>false, 'conditions'=>array('status'=>1,'unit_id'=>null,'country_id'=>$countryId,'eday'=>$eday, AND => array( array('eday >=' => $eday) , array( 'eday <' => $eday+7) )), 'fields'=>'id' ));

Upvotes: 0

Related Questions