Datatables + PHP: Server-Side Processing on Multiple Tables

不打扰是莪最后的温柔 提交于 2021-01-27 02:56:07

问题


How can I get Datatables Server-Side Processing script to work with a custom query? I need to select columns from multiple tables and have Datatables render them.

Datatables.net's Server-Side Processing (SSP) with PHP is summarized here: https://datatables.net/examples/server_side/simple.html

I found this SO question, but the original poster never provided his solution. I don't have sufficient reputation to ask him to provide more detail.

Here is my raw SQL without using Datatable's SSP

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
*/

How can I get Datatables to run queries off the above using SSP?

It appears server_processing.php only accepts 1 table and no custom filtering (i.e., WHERE clauses).

// 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 )
);

However, ssp.class.php does support filtering using WHERE. I'm thinking I need to modify ssp.class.php to force in my WHERE clause

UPDATE

Found a solution. Will post when I have free time.


回答1:


SOLUTION

Class ssp.class.php doesn't support joins and sub-queries, but there is a workaround. The trick is to use sub-query as shown below in $table definition. Replace table with your actual table name in the sub-query.

$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 )
);

You also need to edit ssp.class.php and replace all instances of FROM `$table` with FROM $table to remove backticks.

Make sure all column names are unique otherwise use AS to assign an alias.

NOTES

There is also github.com/emran/ssp repository that contains enhanced ssp.class.php supporting JOINs.

LINKS

See jQuery DataTables: Using WHERE, JOIN and GROUP BY with ssp.class.php for more information.




回答2:


TL;DR: I ended up using a modification of the original Datatables ssp.class.php called ssp.php implemented by Emran Ul Hadi: https://github.com/emran/ssp

His modification accepts JOIN, WHERE, GROUP BY and column aliases. Although the file hasn't been updated in over a year, it still works with DataTables 1.12.x. I made some modifications to his version that increases its robustness and improves the documentation with clearer examples.

Will post my mods/updates here when I have a bit more time. Eventually I hope to put in a pull-request to get my updates into his repository.




回答3:


It looks like the scripts from DataTables are indeed not designed for your particular use case. But there is a method that allows for custom where clauses and from reading the source of ssp.class.php#complex I think that this configuration should work for your by using the WHERE method. The JOIN method will not work here.

Long story short: edit your server_processing.php to that:

<?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);
);

The complex method accepts your custom WHERE clause. But the tricky thing is the usage of 2 tables. This is what the script does not seem to be designed for. I took a look on how it is building the final sql query and it appears that you may just use this table_name.field_name notation in the config, as well as the table_name, table_name notation for the $table and $primaryKey variables.

As mentioned, the usage of 2 tables is not intended by the DataTables script. I don't know if all features of DataTables will work with that.




回答4:


My fork of the original Datatables ssp.class.php: https://github.com/emayskiy/Datatables-SSP-MultiTables

You can alias tables and columns, use where, use [LEFT, INNER, RIGHT, CROSS] JOIN with tables. Example 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:


Sorry to be late, but I was able to solve the problem by building my own server side file, that way I can customize both queries and json outputs.

File serverSide custom:

 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);
        }
    }


来源:https://stackoverflow.com/questions/39183330/datatables-php-server-side-processing-on-multiple-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!