如何在CakePHP中编写STR_TO_DATE()mysql函数?

时间:2018-04-08 09:40:27

标签: cakephp

我在CakePHP中编写了一个查询: -

$customerMovings = $this->Customer->query("SELECT * FROM tbl_customers 
                            AS Customer WHERE Customer.buyer_id = '".$id."' 
                            AND Customer.isCancel = '0' AND Customer.isDeposit = '1' AND Customer.move_date >= '".$moveDate."' 
                            AND Customer.move_date <= '".$move72Date."' 
                            Order by move_date ASC,STR_TO_DATE(move_time,'%l:%i %p')");

如何使用CakePHP ORM查询编写相同的内容?这样的事情: -

$customerMovings = $this->Customer->find('all', array(
    'conditions' => array(
        "Customer.isCancel" =>0,
        "Customer.isDeposit" =>1,
        "Customer.move_date >= " =>$moveDate,
        "Customer.move_date <= " =>$move72Date,
        "OR" => array(
            'Customer.id' =>$selectedCustomer,
            "Customer.creater_id" =>$id
        )
     ),
    'order'=>array(
        "Customer.move_date"=>"ASC",
        "STR_TO_DATE(Customer.move_time,'%l:%i %p')"=>'ASC'
    )
));

1 个答案:

答案 0 :(得分:0)

Cake3解决方案

你可以使用Query对象的func()方法创建任何mysql函数。

在食谱中解释here

  

除了上述功能外,还可以使用func()方法   创建任何通用SQL函数,例如yeardate_formatconvert,   等。

参数作为数组传递,您必须告诉哪些是边界参数,哪些是标识符

所以在你的情况下

->order(function ($exp,  $q) {
    return $q->func()->str_to_date([
        'Customer.move_time' => 'identifiers', // this tells cake that
                                               // Customer.move_time
                                               // is the name of a column and
                                               // not a strimg
        '%l:%i %p'
    ]);
});