1.10 and server-side table joins
1.10 and server-side table joins
Just wondering if anyone has joined tables with 1.10 and server-side. I have come across a situation where I need to join two tables and modifying the default serverside example to do so is a bit beyond the scope of what I know right now. Would love to see an example if anyone has done so.
This discussion has been closed.
Replies
You can drop 1.10 in to the legacy mode by using sAjaxSource or `$.fn.dataTable.ext.legacy.ajax = true;` http://next.datatables.net/manual/server-side#Legacy
Allan
Note that I slightly modified the original script and pass a $myWhere variable which allows me to set custom 'WHERE conditions'. This modification works as expected. I also include my db connections details in a separate file which works of course as well.
the processing php
[code]
<?php
// create condition so logs are shown for active users of active computers only
function active_users_condition($db, $account_id)
{
//get active computers
$active_users = get_active_users($db, $account_id);
//computer_name = 'number1' OR computer_name = 'number2'....
$where_string = array();
foreach($active_users as $key)
{
$where_string[] = "user.user_id = '".$key['user_id']."'";
}
$where_string = implode(" OR ", $where_string);
return $where_string;
}
$where_string = active_users_condition($db, $_SESSION['user']['account_id']);
// DB table to use
$table = 'users, security';
// Table's primary key
$primaryKey = 'users.user_id';
// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes
$columns = array(
array( 'db' => 'security.settings_id', 'dt' => 'settings_id' ),
array( 'db' => 'security.user_id', 'dt' => 'user_id' ),
array( 'db' => 'users.username', 'dt' => 'username' ),
array( 'db' => 'users.computer_name', 'dt' => 'computer_name' ),
array( 'db' => 'security.disable_desktop', 'dt' => 'disable_desktop' ),
array( 'db' => 'security.disable_start', 'dt' => 'disable_start' ),
array( 'db' => 'security.disable_shutdown', 'dt' => 'disable_shutdown' ),
array( 'db' => 'security.disable_run', 'dt' => 'disable_run' ),
array( 'db' => 'security.disable_mouse', 'dt' => 'disable_mouse' ),
array( 'db' => 'security.disable_bootkeys', 'dt' => 'disable_bootkeys' ),
array( 'db' => 'security.disable_cp', 'dt' => 'disable_cp' ),
array( 'db' => 'security.disable_network', 'dt' => 'disable_network' ),
array( 'db' => 'security.disable_taskbar', 'dt' => 'disable_taskbar' ),
array( 'db' => 'security.disable_clock', 'dt' => 'disable_clock' ),
array( 'db' => 'security.disable_logoff', 'dt' => 'disable_logoff' ),
array( 'db' => 'security.disable_startchange', 'dt' => 'disable_startchange' ),
array( 'db' => 'security.disable_taskman', 'dt' => 'disable_taskman' ),
array( 'db' => 'security.disable_clipboard', 'dt' => 'disable_clipboard' ),
array( 'db' => 'security.disable_drives', 'dt' => 'disable_drives' )
);
echo json_encode(
SSP::simple( $_GET, $db, $table, $primaryKey, $columns, "users.user_id = security.user_id AND (".$where_string.")")
//SSP::simple( $_GET, $db, $table, $primaryKey, $columns, "" )
//$myWhere = "" when empty
);
?>
[/code]
in the processing :
[code]
// DB table to use
$table = '`users` LEFT JOIN `security` USING (user_id)';
[/code]
and in ssp.class.php all instances of `$table` (two of them) need to be replaced with just $table. Note that in your processing you are including the backticks in the variable.
I am having problems with multiple column sorting and searching on using joined tables though... any ideas?
Here is what I am trying to accomplish...
[code]
$table = 'users AS u JOIN security AS s ON u.user_id = s.user_id';
// Table's primary key
$primaryKey = 'u.user_id';
// Array of database columns which should be read and sent back to DataTables.
// The `db` parameter represents the column name in the database, while the `dt`
// parameter represents the DataTables column identifier. In this case simple
// indexes
$columns = array(
array( 'db' => 's.settings_id', 'dt' => 'settings_id' ),
array( 'db' => 'u.user_id', 'dt' => 'user_id' ),
array( 'db' => 'u.username', 'dt' => 'username' ),
array( 'db' => 'u.computer_name', 'dt' => 'computer_name' ),
array( 'db' => 's.disable_desktop', 'dt' => 'disable_desktop' ),
array( 'db' => '.disable_start', 'dt' => 'disable_start' ),
array( 'db' => 's.disable_shutdown', 'dt' => 'disable_shutdown' ),
array( 'db' => 's.disable_run', 'dt' => 'disable_run' ),
array( 'db' => 's.disable_mouse', 'dt' => 'disable_mouse' ),
array( 'db' => 's.disable_bootkeys', 'dt' => 'disable_bootkeys' ),
array( 'db' => 's.disable_cp', 'dt' => 'disable_cp' ),
array( 'db' => 's.disable_network', 'dt' => 'disable_network' ),
array( 'db' => 's.disable_taskbar', 'dt' => 'disable_taskbar' ),
array( 'db' => 's.disable_clock', 'dt' => 'disable_clock' ),
array( 'db' => 's.disable_logoff', 'dt' => 'disable_logoff' ),
array( 'db' => 's.disable_startchange', 'dt' => 'disable_startchange' ),
array( 'db' => 's.disable_taskman', 'dt' => 'disable_taskman' ),
array( 'db' => 's.disable_clipboard', 'dt' => 'disable_clipboard' ),
array( 'db' => 's.disable_drives', 'dt' => 'disable_drives' )
);
echo json_encode(
SSP::simple( $_GET, $db, $table, $primaryKey, $columns)
);
[/code]
Current status of this is no error from firebug, however, all columns are returned as 'null' so the table shows with empty columns in its rows. draw, recordsTotal, and recordsFiltered are being returned properly. Searching also appears to work - since I know what the values in the empty columns 'should' be I can search for terms and it correctly redraws the table to only show those rows... albeit they are empty of course. Loading the table shows 5 rows of empty columns... I search for say 'joe' which only appears in one column of one row and the table redraws to show one row of empty columns and updates the 'showing' and 'filtered from' results correctly.
Seems like this is something easy I am missing so if anyone cares to look that would be great. I am sure this would be useful to many once working properly.
[code]
array( 'db' => '`security`.`settings_id`', 'dt' => 'settings_id' ),
or
array( 'db' => '`security`.`settings_id`', 'dt' => 5),
[/code]
Allan, would you be interested in taking a look at this as I am sure it can be cleaned up? This could then be another option to include for those needing table joins and where conditions on the data being requested. I actually have it setup in a way that it works without table joins or where conditions as well you just define them as empty "" if not using them.
Allan