CakePHP - select 来自有条件的数据库

CakePHP - select from database with condition

我有这个select或者:

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

这很完美。但现在我需要另一个,但我不知道该怎么做 ;)

我需要 select 来自 Table 的所有记录,但条件是:

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

这个方法简单吗?也许这是个愚蠢的问题,但我在 PHP 中没有 exp ;)

只需在当前条件数组中传递一个 AND 数组即可:

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

在 cakephp 中,and 条件的等效项是
示例:column1 = 'value' AND column2 = 'value'

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

所以您的查询构建器会像这样

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

你试过这样的事情吗:

$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'])

这看起来更干净。