在cakephp 3 beta的条件之间

时间:2015-01-25 09:42:09

标签: model between cakephp-3.0

cakephp 3

中此代码的等价物
SELECT column_name(s)
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

我试试这个

$condition["Advertisements.created BETWEEN ? AND ?"] = [1418874212 , 1418889212];

但它不起作用

2 个答案:

答案 0 :(得分:0)

            $Query = $Table
            ->find('all')
            ->where([
                'date BETWEEN :start_date AND :end_date'   
            ])

            ->bind(':start_date ',$s_date)
            ->bind(':end_date',$e_days); 

            $Query->hydrate(false);
            $result = $Query->toArray();

答案 1 :(得分:0)

您可以使用以下两种方法之一。

方法1:

$start_date = '2015-11-01 00:00:00';
$end_date = '2015-11-01 23:59:59';
$query = $this->Advertisements->find('all')
         ->where(function ($exp, $q) use($start_date,$end_date) {
             return $exp->between('created', $start_date, $end_date);
         });
$result = $query->toArray();

方法2:

$start_date = '2015-11-01 00:00:00';
$end_date = '2015-11-01 23:59:59';
$query = $this->Advertisements->find('all')
         ->where([
            'created BETWEEN :start AND :end'
         ])
         ->bind(':start', new \DateTime($start_date), 'datetime')
         ->bind(':end',   new \DateTime($end_date), 'datetime');
$result = $query->toArray();