php - CakePHP 3 Paginator 自定义查询(存储过程)从结果中删除列

标签 php mysql cakephp pagination cakephp-3.0

我正在尝试在 CakePHP 3 中对 MySQL 存储过程的结果进行分页。目前,我的存储过程按预期工作,我可以在 CakePHP 中成功调用它,并且分页器几乎按预期工作。它正在删除一列 (customer_name),并显示 user_id 而不是给定 user_id 的用户名。我正在使用CacheSP插件来调用SP。 https://github.com/genellern/Cakephp3-CacheSP

这是我的存储过程:

CREATE DEFINER=`root`@`localhost` PROCEDURE `ordersIndex`()
BEGIN
SELECT o.id, o.order_number, DATE_FORMAT(o.created, '%m/%d/%y %h:%s %p') AS 
created, d.customer_name, o.order_amount, u.username, o.status FROM orders AS o 
INNER JOIN order_details AS d
ON o.order_number=d.order_number
INNER JOIN users as u
ON o.created_by=u.id;
END

以下是调用存储过程的代码:

$orderQuery =  $this->Orders->callSP('ordersIndex');
$orderRecords = $orderQuery->fetchAll('assoc');
debug($orderRecords);
$orderQuery->closeCursor();

产生这个结构:

[
  (int) 0 => [
    'id' => '43',
    'order_number' => '51',
    'created' => '02/17/16 06:10 PM',
    'customer_name' => 'cust test',
    'order_amount' => '72015.00',
    'username' => 'BSounder',
    'status' => 'pending'
  ],
  (int) 1 => [
    'id' => '44',
    'order_number' => '84',
    'created' => '02/18/16 05:06 PM',
    'customer_name' => 'cust test',
    'order_amount' => '500.00',
    'username' => 'BSounder',
    'status' => 'pending'
  ],
....
]

为了让分页器与自定义查询一起工作,我跟踪/调整了此链接中的两个函数:http://technet.weblineindia.com/web/pagination-with-custom-queries-in-cakephp/ 这导致了这些功能:

public function paginate($conditions, $fields, $order, $limit, $page =1, 
        $recursive = null, $extra = array())
{
    $recursive = -1;
    $this->useTable = false;
    $results = $this->Orders->callSP('ordersIndex');
    debug($results);
    return $results;
}

public function paginateCount($conditions = null, $recursive = 0, $extra = array())
{
    $this->recursive = $recursive;
    $results = $this->Orders->callSP('ordersIndex');
    return count($results);
}

查看订单/索引时,除了“客户名称”列为空且“创建者”列显示 user_id 而不是用户的用户名外,一切正常。任何帮助将非常感激! :)

最佳答案

我放弃使用 Cake 的默认分页。相反,我使用了 DataTables,到目前为止我对它非常满意。我可以根据存储过程的结果填充表,然后 Datatables 处理分页。甚至包括搜索和更改每页结果的数量。

https://datatables.net/

关于php - CakePHP 3 Paginator 自定义查询(存储过程)从结果中删除列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35731903/

相关文章:

php - 在 CakePHP 中将斜杠参数传递给 requestAction

node.js - 如何通过cakePHP访问NodeJS数据?

php - imagettftext 无法打开字体文件

mysql - 良好的数据库设计/规范化

javascript - 在 html <div> 中显示 php 结果?

php - cakephp 检查重复行

PHP - 两个多选下拉菜单,将用户选择传递给 MySQL 查询

php - 如何在页面中间用 PHP 附加到 <head>?

php - 如何使用 cURL 创建子域?

mysql - 从 SQL Server(ODBC 连接)使用 MySQL 作为链接服务器时查询缓慢