Cakephp 2 Paginate最近的订单

时间:2013-01-31 17:56:58

标签: cakephp-2.1

我正在尝试从CakePhp 2.x应用程序中的Orders表中获取结果列表,以便按最接近今天的日期对自己进行排序。

在通常的mysql查询中,我有类似的工作,请说出以下语法:

ABS(DATEDIFF(Order.duedate, NOW()))

但是在Cake中我不知道如何在分页助手中使用这样的自定义查询。这可能是我需要在模型中的查找器查询中设置的吗?

这是我目前拥有的控制器(设置为沼泽标准降序排序)

    public function index() {
           $this->paginate = array( 
                    'conditions' => array (
                        'Order.despatched' => array('Not Despatched' , 'Split Despatched')
                    ),
                    'limit' => 25,
                    'order' => array('Order.duedate' => 'DESC')
            );

           $data = $this->paginate('Order');
           $this->set('orders', $data);
}

编辑:使用下面评论中的信息,我在控制器中添加了一个虚拟字段,导致sql错误。起初我认为这是由于模型中的关联,试图纠正这个问题我将virtualField添加到Order Model中并添加到相关Account Model的构造函数中。然而,这没有改变sql中断的部分。完整的SQL错误是:

Error: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AS `Order__closestdate`, `Account`.`id`, `Account`.`company`, `Account`.`contac' at line 1

SQL Query: SELECT `Order`.`id`, `Order`.`order_id`, `Order`.`orderdate`,
 `Order`.`duedate`, `Order`.`rework`, `Order`.`rework_notes`, `Order`.`comments`, 
`Order`.`status`, `Order`.`customer`, `Order`.`last_edited`, `Order`.`value`, 
`Order`.`quantity`, `Order`.`order_for`, `Order`.`warehouse`, `Order`.`haulier`, 
`Order`.`terms`, `Order`.`type`, `Order`.`despatched`, `Order`.`despatched_date`, 
`Order`.`invoiced`, `Order`.`invoice_date`, `Order`.`bookingref`, 
`Order`.`purchaseref`, `Order`.`payment_due`, `Order`.`payment_status`, 
`Order`.`payment_date`, (ABS(DATEDIFF(duedate, NOW())) AS `Order__closestdate`, 
`Account`.`id`, `Account`.`company`, `Account`.`contact`, `Account`.`phone`, 
`Account`.`email`, `Account`.`postcode`, `Account`.`created`, `Account`.`modified`, 
`Account`.`customer`, `Account`.`address1`, `Account`.`address2`, `Account`.`town`, 
`Account`.`county`, (ABS(DATEDIFF(duedate, NOW())) AS `Account__closestdate` FROM 
`hunter_om`.`orders` AS `Order` LEFT JOIN `hunter_om`.`accounts` AS `Account` ON 
(`Order`.`customer` = `Account`.`customer`) WHERE `Order`.`despatched` IN ('Not 
Despatched', 'Split Despatched') ORDER BY (ABS(DATEDIFF(duedate, NOW())) DESC LIMIT 25

作为参考,模型中的代码为:

//Order Model
public $virtualFields = array(
   'closestdate' => 'ABS(DATEDIFF(duedate, NOW())'
);

//Account Model
  public function __construct($id = false, $table = null, $ds = null) {
   parent::__construct($id, $table, $ds);
   $this->virtualFields['closestdate'] = $this->Order->virtualFields['closestdate'];
   }

感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

在分页之前,您需要在Order模型中添加'virtualField';

$this->Order->virtualFields['closestdate'] = "ABS(DATEDIFF(Order.duedate, NOW()))";

然后将该字段用作查询/分页中的常规字段

public function index() {

    $this->Order->virtualFields['closestdate'] = "ABS(DATEDIFF(Order.duedate, NOW()))";

    $this->paginate = array( 
        'conditions' => array (
            'Order.despatched' => array('Not Despatched' , 'Split Despatched')
        ),
        'limit' => 25,
        'order' => array('Order.closestdate' => 'DESC')
    );

    $data = $this->paginate('Order');
    $this->set('orders', $data);
}