【问题标题】:Datatables + PHP: Server-Side Processing on Multiple TablesDatatables + PHP:多表的服务器端处理
【发布时间】:2017-01-04 03:09:51
【问题描述】:

如何让 Datatables 服务器端处理 脚本与自定义查询一起工作?我需要从多个表中选择列并让 Datatables 呈现它们。

Datatables.net 使用 PHP 的服务器端处理 (SSP) 总结如下:https://datatables.net/examples/server_side/simple.html

我找到了这个SO question,但原始发帖人从未提供过他的解决方案。我没有足够的声誉要求他提供更多细节。

这是我没有使用 Datatable 的 SSP 的原始 SQL

SELECT tbl_houses.style, tbl_houses.roomCount, tbl_residents.firstName, tbl_residents.lastName
FROM tbl_houses, tbl_residents
WHERE tbl_houses.houseID = tbl_residents.residentID

/* 
* # Equivalent query using JOIN suggested by @KumarRakesh
* # Note: JOIN ... ON is a synonym for INNER JOIN ... ON
* # Using JOIN conforms to syntax spec'd by ANSI-92 https://stackoverflow.com/a/894855/946957
*
* SELECT tbl_houses.style, tbl_houses.roomCount, tbl_residents.firstName, tbl_residents.lastName 
* FROM tbl_houses 
* JOIN tbl_residents ON tbl_houses.houseID = tbl_residents.residentID
*/

如何让 Datatables 使用 SSP 运行上述查询?

server_processing.php 似乎只接受 1 个表并且没有自定义过滤(即WHERE 子句)。

// DB table to use
$table = 'datatables_demo';

// Table's primary key
$primaryKey = 'id';

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * If you just want to use the basic configuration for DataTables with PHP
 * server-side, there is no need to edit below this line.
 */

require( 'ssp.class.php' );

echo json_encode(
    SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);

但是,ssp.class.php确实支持使用 WHERE 进行过滤。我想我需要修改ssp.class.php 以强制我的WHERE 子句

更新

找到了解决办法。有空会发帖。

【问题讨论】:

  • 这种情况下你使用join查询了吗
  • 你也可以这样做.. SELECT tbl_house.style, tbl_house.roomCount, tbl_residents.firstName, tbl_residents.lastName FROM tbl_house JOIN tbl_resident ON tbl_house.houseID = tbl_resident.residentID
  • @KumarRakesh 我试图弄清楚如何将自定义 SQL 查询塞进 Datatables 服务器端处理 (SSP) 脚本中。我的问题不是如何构建 SQL 查询,而是如何将其合并到服务器端的 Datatables 的 SSP“框架”中。
  • 我想我必须将我的自定义过滤器(即 WHERE 子句)硬连接到 sss.class.php 文件中。如果可行,将尝试然后发布更新
  • 我为服务器端数据表开发了一个类,它依赖于 PDO 适配器,自从 6 年以来,我几乎在所有项目中都完美使用过,在看到你的问题后,我只是在 Github 上托管了这个,这里是链接如果它对您有帮助github.com/oromedialab/datatables 随时提出问题并尽我所能做出回应,它支持任何加入、groupby 等。

标签: php mysql datatables server-side


【解决方案1】:

解决方案

ssp.class.php 类不支持连接和子查询,但有一种解决方法。诀窍是使用如下$table 定义中所示的子查询。将table 替换为子查询中的实际表名。

$table = <<<EOT
 (
    SELECT 
      a.id, 
      a.name, 
      a.father_id, 
      b.name AS father_name
    FROM table a
    LEFT JOIN table b ON a.father_id = b.id
 ) temp
EOT;

$primaryKey = 'id';

$columns = array(
   array( 'db' => 'id',          'dt' => 0 ),
   array( 'db' => 'name',        'dt' => 1 ),
   array( 'db' => 'father_id',   'dt' => 2 ),
   array( 'db' => 'father_name', 'dt' => 3 )
);

$sql_details = array(
   'user' => '',
   'pass' => '',
   'db'   => '',
   'host' => ''
);

require( 'ssp.class.php' );
echo json_encode(
   SSP::simple( $_GET, $sql_details, $table, $primaryKey, $columns )
);

您还需要编辑ssp.class.php 并将FROM `$table` 的所有实例替换为FROM $table 以删除反引号。

确保所有列名都是唯一的,否则使用AS 分配别名。

注意事项

还有 github.com/emran/ssp 存储库,其中包含增强的 ssp.class.php 支持 JOIN。

链接

更多信息请参见jQuery DataTables: Using WHERE, JOIN and GROUP BY with ssp.class.php。

【讨论】:

    【解决方案2】:

    TL;DR:我最终使用了由 Emran Ul Hadi 实现的名为 ssp.php 的原始数据表 ssp.class.php 的修改:https://github.com/emran/ssp

    他的修改接受 JOIN、WHERE、GROUP BY 和列别名。尽管该文件一年多没有更新,但它仍然适用于 DataTables 1.12.x。我对他的版本进行了一些修改,以增加其健壮性并通过更清晰的示例改进文档。

    当我有更多时间时,我会在这里发布我的模组/更新。最终,我希望提出一个拉取请求,将我的更新放到他的存储库中。

    【讨论】:

    • 您是否尝试过修改它以支持“UNION ALL”?
    • @Milson 的意思是将草稿发布到 Github,并向 Emran 提出拉取请求。我的 mod 主要是简化用于调用 ssp 函数/过程的语法和文档清晰度。添加UNION 功能应该是微不足道的。查看 Emran 为打开 JOIN 所做的更改,了解如何实现其他 SQL 函数
    【解决方案3】:

    看起来 DataTables 中的脚本确实不是为您的特定用例设计的。但是有一种方法允许自定义 where 子句和读取 ssp.class.php#complex 的源代码,我认为这种配置应该适合您使用 WHERE 方法。 JOIN 方法在这里不起作用。

    长话短说:将您的 server_processing.php 编辑为:

    <?php
    // DB table to use
    $table = 'tbl_houses, tbl_residents';
    
    // First table's primary key
    $primaryKey = 'tbl_houses.id';
    
    $columns = [
        [ 'db' => 'tbl_houses.style'],
        [ 'db' => 'bl_houses.roomCount'],
        [ 'db' => 'tbl_residents.firstName'],
        [ 'db' => 'tbl_residents.lastName']
    
    );
    
    // connection details
    $sql_details = [
    
    ];
    
    $whereAll = 'tbl_houses.houseID = tbl_residents.residentID';
    
    require( 'ssp.class.php' );
    
    echo json_encode(
        SSP::complex( $_GET, $sql_details, $table, $primaryKey, $columns , null, $whereAll);
    );
    

    complex 方法接受您的自定义 WHERE 子句。但棘手的是使用 2 个表。这似乎不是脚本设计的目的。我看了一下它是如何构建最终的 sql 查询的,看来您可以在配置中使用这个 table_name.field_name 符号,以及 $table 和 $primaryKey 变量的 table_name, table_name 符号。

    如前所述,DataTables 脚本不打算使用 2 个表。我不知道 DataTables 的所有功能是否都适用。

    【讨论】:

    • 感谢您的深入了解!我实施了你的建议,你是对的!数据表ssp.class.php不支持开箱即用的多个表。也就是说,$table = 'tbl_houses, tbl_residents' 产生 Base table or view not found: 1146 错误。不过,我确实找到了解决方案,并会在我有空闲时间时发布。
    【解决方案4】:

    我对原始 Datatables ssp.class.php 的 fork:https://github.com/emayskiy/Datatables-SSP-MultiTables

    您可以给表和列取别名,使用 where,将 [LEFT, INNER, RIGHT, CROSS] JOIN 与表一起使用。 示例 server_processing.php:

    <?php 
        //For one table:
        //$table = "main_table_name";
        //Or for multitable queries: 
        $table = array(
            array('table'=>'main_table_name',  'as'=>'mt'),    
            array('table'=>'join1_table_name', 'as'=>'jt1', 'join_type'=>'INNER', 'join_on'=>'mt.field  = jt1.field'),
            array('table'=>'join2_table_name', 'as'=>'jt2', 'join_type'=>'LEFT',  'join_on'=>'mt.field1 = jt2.field')
        ); 
        //Columns definition with alias
        $columns = array(
            array( 'db' => 'mt.field1',  'dt' => 0 ),
            array( 'db' => 'mt.field2',  'dt' => 1 ),    
            array( 'db' => 'mt.name',    'as'=>'field3', 'dt' => 2 ),    
            array( 'db' => 'jt1.field1', 'as'=>'field4', 'dt' => 3 ),
            array( 'db' => 'jt2.field5', 'as'=>'field5', 'dt' => 4 )       
        );
        $primaryKey = 'mt.id'; //Primary key, for check records count
        $sql_details = array(
            'user' => $db_user,
            'pass' => $db_password,
            'db'   => $db_name,
            'host' => $db_host
        );
        $where = "mt.field1 > 10"; //You SQL where condition
    
        require('ssp.class.php' ); //File from github.com/emayskiy/Datatables-SSP-MultiTables
        echo json_encode(
            // All params same as in original class SSP
            SSP::complex( $_GET, $sql_details, $table, $primaryKey, $columns , '', $where)
        );
    
    ?>
    

    【讨论】:

      【解决方案5】:

      很抱歉迟到了,但我能够通过构建自己的服务器端文件来解决问题,这样我就可以自定义查询和 json 输出。

      文件服务器端自定义:

       public function produtosEstoque($arr)
          {
                    
              //COLUNAS
              $column = array('prodNome', 'prodPreco', 'prodQtdEst', 'categDescricao');
      
      
              //SQL
              $query2 = "SELECT a.prodID, a.prodNome, a.prodMedida, a.prodPrecoAnt, a.prodPreco, a.prodCategoriaID, a.prodOferta,a.prodQtdEst, a.prodQtdMEst, b.categDescricao FROM produtos a INNER JOIN categoria b ON a.prodCategoriaID = b.categID WHERE a.prodEstatusID = 6 ";
      
              //SEARCH
              if ($arr['search']['value']) {
                  $query2 .= "AND prodNome LIKE '%".$arr['search']['value']."%' ";
              }
      
              //ORDER
              if (isset($arr['order'])) {
                  $query2 .= 'ORDER BY ' . $column[$arr['order']['0']['column']] . ' ' . $arr['order']['0']['dir'] . ' ';
              } else {
                  $query2 .= 'ORDER BY a.prodID DESC ';
              }
      
              
              //LIMIT
              if ($arr["length"] != -1) {
                  $query3 = 'LIMIT ' . $arr['start'] . ', ' . $arr['length'];
              }
              
              
              try {
                  
                  //TOTAL DE REGISTROS NA TABELA
                  $query1 = "SELECT * FROM produtos WHERE prodEstatusID = 6";
                  $stm1 = $this->pdo->prepare($query1);
                  $stm1->execute();
                  $contReg = $stm1->rowCount($stm1);   
                  
                  
                  $stm = $this->pdo->prepare($query2);
                  $stm->execute();
                  $number_filter_row = $stm->rowCount($stm);
      
      
                  $stm = $this->pdo->prepare($query2 . $query3);
                  $stm->execute();
                  $list = $stm->fetchAll(PDO::FETCH_OBJ);
      
      
                  $data = [];
                  foreach ($list as $row) {                                  
                      $data[] = array('prod_nome' => $row->prodNome . ", " . $row->prodMedida, 'prod_preco' => "R$ ".$this->convInReal($row->prodPreco)." ".$this->formatProdOff($row->prodOferta), 'prod_estoque' => $row->prodQtdEst . " - M: " . $row->prodQtdMEst, 'prod_categoria' => $row->categDescricao);                                
                  }
      
                  $dat = array('draw' => intval($arr["draw"]), 
                        'recordsTotal' => $contReg, 
                        'recordsFiltered' => $number_filter_row, 
                        'data' => $data
                        );
                  
                  return json_encode($dat, JSON_UNESCAPED_UNICODE);
      
              } catch (PDOException $erro) {
                  $data = array('msgEr' => 'ERR_002_EI', 'erroLine' => $erro->getLine());
                  return json_encode($data);
              }
          }
      

      【讨论】:

        【解决方案6】:

        我通过在 mysql phpmyadmin 中创建视图表解决了这个问题。

        在phpmyadmin中运行你的sql脚本然后点击create view

        enter image description here

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-05-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-01-13
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多