0

我试图从我的 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 添加到订单模型和关联帐户模型的构造函数中。但是,这对中断的 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'];
   }

感谢任何帮助。

4

1 回答 1

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);
}
于 2013-02-03T15:24:26.600 回答