Note that there are some explanatory texts on larger screens.

plurals
  1. POServerside Datatables finds entries marked as deleted
    primarykey
    data
    text
    <p>I am using Server-side Datatables (and CodeIgniter) to show data.</p> <p>I can't directly delete entries from the Database (guideline of the project, unfortunately), so I am working with deleted-Flags, a column in the DB table called 'deleted' (tinyint(1) since MySQL automatically changed it from BOOLEAN to that).</p> <p>For the server side processing I'm using the script provided at <a href="https://github.com/blake-nouribekian/codeigniter-datatables/blob/master/data.php" rel="nofollow">https://github.com/blake-nouribekian/codeigniter-datatables/blob/master/data.php</a>, with some small changes</p> <pre><code>// Columns use for SELECT part of the query. These names are not being escaped, so subqueries etc are possible. $aColumns = array('events.id AS id', 'title', "FROM_UNIXTIME(start, '%d.%m.%Y') AS start", "FROM_UNIXTIME(start, '%H:%i') AS start_time", '(SELECT COUNT(*) FROM event_bookings WHERE id_event = events.id AND confirmed IS NOT NULL) AS participants', 'max_participants'); // Column names for WHERE clause (important for stuff like subqueries). $aColumnNames = array('id', 'title', "start", "start", '(SELECT COUNT(*) FROM event_bookings WHERE id_event = events.id AND confirmed IS NOT NULL)', 'max_participants'); // Column for deleted flag in the DB. $deleted_column = 'deleted'; // DB table to use $sTable = 'events'; // no deleted entries $this-&gt;db-&gt;where(Event_Model::$dbDeletedFlag, 0); </code></pre> <p>Initially, the data is shown correct, no entries marked as deleted show up. As soon as I type something into the search field, however, it finds ALL entries that match '... LIKE %input%'. I've gone wild with the $this->db->where(Event_Model::$dbDeletedFlag, 0); command, placing it at every possible line, still no improvement. It shows the entries marked as deleted. The where commands somehow don't work, when the search field is used.</p> <p>Any suggestions how to solve this?</p> <p><strong>EDIT:</strong></p> <p>Thanks for the answers so far. I'm posting the whole function right here:</p> <pre><code>public function server_processing() { // EDIT THIS // ----- // Columns use for SELECT part of the query. These names are not being escaped, so subqueries etc are possible. $aColumns = array('events.id AS id', 'title', "FROM_UNIXTIME(start, '%d.%m.%Y') AS start", "FROM_UNIXTIME(start, '%H:%i') AS start_time", '(SELECT COUNT(*) FROM event_bookings WHERE id_event = events.id AND confirmed IS NOT NULL) AS participants', 'max_participants'); // Column names for WHERE clause (important for stuff like subqueries). $aColumnNames = array('id', 'title', "start", "start", '(SELECT COUNT(*) FROM event_bookings WHERE id_event = events.id AND confirmed IS NOT NULL)', 'max_participants'); // Column for deleted flag in the DB. $deleted_column = 'deleted'; // DB table to use $sTable = 'events'; // no deleted entries $this-&gt;db-&gt;where(Event_Model::$dbDeletedFlag, 0); // ------- $iDisplayStart = $this-&gt;input-&gt;get_post('iDisplayStart', true); $iDisplayLength = $this-&gt;input-&gt;get_post('iDisplayLength', true); $iSortCol_0 = $this-&gt;input-&gt;get_post('iSortCol_0', true); $iSortingCols = $this-&gt;input-&gt;get_post('iSortingCols', true); $sSearch = $this-&gt;input-&gt;get_post('sSearch', true); $sEcho = $this-&gt;input-&gt;get_post('sEcho', true); // Paging if (isset($iDisplayStart) &amp;&amp; $iDisplayLength != '-1') { $this-&gt;db-&gt;limit($this-&gt;db-&gt;escape_str($iDisplayLength), $this-&gt;db-&gt;escape_str($iDisplayStart)); } // Ordering if (isset($iSortCol_0)) { for ($i = 0; $i &lt; intval($iSortingCols); $i++) { $iSortCol = $this-&gt;input-&gt;get_post('iSortCol_' . $i, true); $bSortable = $this-&gt;input-&gt;get_post('bSortable_' . intval($iSortCol), true); $sSortDir = $this-&gt;input-&gt;get_post('sSortDir_' . $i, true); if ($bSortable == 'true') { $this-&gt;db-&gt;order_by($aColumnNames[intval($this-&gt;db-&gt;escape_str($iSortCol))], $this-&gt;db-&gt;escape_str($sSortDir)); } } } /* * Filtering * NOTE this does not match the built-in DataTables filtering which does it * word by word on any field. It's possible to do here, but concerned about efficiency * on very large tables, and MySQL's regex functionality is very limited */ if (isset($sSearch) &amp;&amp; !empty($sSearch)) { for ($i = 0; $i &lt; count($aColumns); $i++) { $bSearchable = $this-&gt;input-&gt;get_post('bSearchable_' . $i, true); // Individual column filtering if (isset($bSearchable) &amp;&amp; $bSearchable == 'true') { $this-&gt;db-&gt;or_like($aColumnNames[$i], $this-&gt;db-&gt;escape_like_str($sSearch)); } } } // Select Data $this-&gt;db-&gt;select('SQL_CALC_FOUND_ROWS ' . str_replace(' , ', ' ', implode(', ', $aColumns)), false); $rResult = $this-&gt;db-&gt;get($sTable); // Data set length after filtering $this-&gt;db-&gt;select('FOUND_ROWS() AS found_rows'); $iFilteredTotal = $this-&gt;db-&gt;get()-&gt;row()-&gt;found_rows; // Total data set length $iTotal = $this-&gt;db-&gt;count_all($sTable); // Output $output = array( 'sEcho' =&gt; intval($sEcho), 'iTotalRecords' =&gt; $iTotal, 'iTotalDisplayRecords' =&gt; $iFilteredTotal, 'aaData' =&gt; array() ); $output['aaData'] = array(); foreach ($rResult-&gt;result_array() as $aRow) { // EDIT THIS // -------- $row = array( '&lt;a class="detail_link link" href="javascript:void(0);" data-event-id="' . $aRow['id'] . '"&gt;' . $aRow['title'] . '&lt;/a&gt;', $aRow['start'], $aRow['start_time'], $aRow['participants'] . ' / ' . $aRow['max_participants'] ); if ($this-&gt;auth-&gt;access(Auth::Admin, true, false)) { $row[] = '&lt;img class="edit_link" data-event-id="' . $aRow['id'] . '" src="' . base_url() . 'img/icons/glyphicons_150_edit.png"&gt; &lt;img class="delete_link" data-event-id="' . $aRow['id'] . '" src="' . base_url() . 'img/icons/glyphicons_192_circle_remove.png"&gt;'; } // -------- $output['aaData'][] = $row; } echo json_encode($output); } </code></pre> <p>Thank you for your efforts!</p> <p><strong>EDIT 2</strong></p> <p>I think, the problem is, that the generated SQL-Statement looks like this, when the Search field is used:</p> <pre><code>SELECT fields FROM table WHERE deleted = 0 OR field LIKE %input% </code></pre> <p>Whereas it rather should be:</p> <pre><code>SELECT fields FROM table WHERE field LIKE %input% AND deleted = 0 </code></pre> <p>I just don't know really, how the Active records build the SQL statements, in order to solve this problem</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload