服务器端数据表查找标记为已删除的条目

时间:2013-03-27 10:39:57

标签: php jquery codeigniter activerecord datatables

我正在使用服务器端数据表(和CodeIgniter)来显示数据。

我不能直接删除数据库中的条目(项目指南,不幸的是),所以我正在使用deleted-Flags,DB表中的一个名为'deleted'的列(tinyint(1),因为MySQL自动更改它从BOOLEAN到那个。)

对于服务器端处理我正在使用https://github.com/blake-nouribekian/codeigniter-datatables/blob/master/data.php提供的脚本,并进行了一些小的更改

// 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->db->where(Event_Model::$dbDeletedFlag, 0);

最初,数据显示正确,没有标记为已删除的条目显示。但是,只要我在搜索字段中输入内容,就会找到与“... LIKE%input%”匹配的所有条目。我已经疯狂了$ this-> db-> where(Event_Model :: $ dbDeletedFlag,0);命令,把它放在每一条可能的线上,仍然没有改进。它显示标记为已删除的条目。当使用搜索字段时,where命令以某种方式不起作用。

有任何建议如何解决这个问题?

修改

感谢目前为止的答案。我在这里发布了整个功能:

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->db->where(Event_Model::$dbDeletedFlag, 0);
    // -------

    $iDisplayStart = $this->input->get_post('iDisplayStart', true);
    $iDisplayLength = $this->input->get_post('iDisplayLength', true);
    $iSortCol_0 = $this->input->get_post('iSortCol_0', true);
    $iSortingCols = $this->input->get_post('iSortingCols', true);
    $sSearch = $this->input->get_post('sSearch', true);
    $sEcho = $this->input->get_post('sEcho', true);

    // Paging
    if (isset($iDisplayStart) && $iDisplayLength != '-1') {
      $this->db->limit($this->db->escape_str($iDisplayLength), $this->db->escape_str($iDisplayStart));
    }

    // Ordering
    if (isset($iSortCol_0)) {
      for ($i = 0; $i < intval($iSortingCols); $i++) {
        $iSortCol = $this->input->get_post('iSortCol_' . $i, true);
        $bSortable = $this->input->get_post('bSortable_' . intval($iSortCol), true);
        $sSortDir = $this->input->get_post('sSortDir_' . $i, true);

        if ($bSortable == 'true') {
          $this->db->order_by($aColumnNames[intval($this->db->escape_str($iSortCol))], $this->db->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) && !empty($sSearch)) {
      for ($i = 0; $i < count($aColumns); $i++) {
        $bSearchable = $this->input->get_post('bSearchable_' . $i, true);

        // Individual column filtering
        if (isset($bSearchable) && $bSearchable == 'true') {
          $this->db->or_like($aColumnNames[$i], $this->db->escape_like_str($sSearch));
        }
      }
    }

    // Select Data
    $this->db->select('SQL_CALC_FOUND_ROWS ' . str_replace(' , ', ' ', implode(', ', $aColumns)), false);
    $rResult = $this->db->get($sTable);


    // Data set length after filtering
    $this->db->select('FOUND_ROWS() AS found_rows');
    $iFilteredTotal = $this->db->get()->row()->found_rows;

    // Total data set length
    $iTotal = $this->db->count_all($sTable);

    // Output
    $output = array(
        'sEcho' => intval($sEcho),
        'iTotalRecords' => $iTotal,
        'iTotalDisplayRecords' => $iFilteredTotal,
        'aaData' => array()
    );

    $output['aaData'] = array();
    foreach ($rResult->result_array() as $aRow) {
      // EDIT THIS
      // --------
      $row = array(
          '<a class="detail_link link" href="javascript:void(0);" data-event-id="' . $aRow['id'] . '">' . $aRow['title'] . '</a>',
          $aRow['start'],
          $aRow['start_time'],
          $aRow['participants'] . ' / ' . $aRow['max_participants']
      );
      if ($this->auth->access(Auth::Admin, true, false)) {
        $row[] = '<img class="edit_link" data-event-id="' . $aRow['id'] . '" src="' . base_url() . 'img/icons/glyphicons_150_edit.png"> <img class="delete_link" data-event-id="' . $aRow['id'] . '" src="' . base_url() . 'img/icons/glyphicons_192_circle_remove.png">';
      }
      // --------
      $output['aaData'][] = $row;
    }

    echo json_encode($output);
  }

感谢您的努力!

编辑2

我认为,问题是,当使用搜索字段时,生成的SQL-Statement看起来像这样:

SELECT fields FROM table WHERE deleted = 0 OR field LIKE %input%

而它应该是:

SELECT fields FROM table WHERE field LIKE %input% AND deleted = 0

我真的不知道,Active Record如何构建SQL语句,以解决这个问题

1 个答案:

答案 0 :(得分:1)

根据您的评论,问题似乎在于Active Record如何构建LIKE子句的WHERE片段。

if (isset($sSearch) && !empty($sSearch)) {
    // Build up the SQL manually. First, initialize SQL string.  
    // This clause will be ANDed to the other WHERE clauses; 
    // Use parentheses to isolate the OR in the inner clauses
    $sql = "("; // 
    for ($i = 0; $i < count($aColumns); $i++) {
      $bSearchable = $this->input->get_post('bSearchable_' . $i, true);

      // Individual column filtering
      if (isset($bSearchable) && $bSearchable == 'true') {
        //$this->db->or_like($aColumnNames[$i], $this->db->escape_like_str($sSearch));
        $sql_tag = ($i==0) ? "" : " OR "; // don't need OR before the first field
        $sql .= $sql_tag . $aColumnNames[$i] . " LIKE " . $this->db->escape_like_str($sSearch);
      }
    }
    $sql .= ")"; // close parentheses
    $this->db->where($sql);  // this will add to the WHERE clause using AND
}

我要做的是手动建立LIKE子句。您需要允许多个字段可搜索。如果我得到了正确的详细信息,那么您的最终查询应该类似于:

SELECT fields FROM table WHERE deleted = 0 
  AND (field1 LIKE '%input%' OR field2 LIKE '%input%' OR field3 LIKE '%input%')

假设您有三个要搜索的字段。