zepernick / Codeigniter-DataTables

CodeIgniter Library For Ajax Server Side DataTables 1.10 >
MIT License
94 stars 95 forks source link

Asking for whereClauseArray() #12

Closed dodeagung closed 8 years ago

dodeagung commented 8 years ago

Hi Zepernick,

Your plugin superb, thanks very much for your effort to write that!

However, I want to ask you about whereClauseArray(). In you explanation of "Advanced DatatableModel Implementation", you wrote:

'u.id' => $this -> ion_auth -> get_user_id()

can you provide me the details explanation about this line of code?

Since I have similar case, which is the u.Id value would change based on user input.

That's all I want to know.

Best, Agung

3s2ng commented 8 years ago

This module is God send.

I just want to know how to implement the whereClauseArray dynamically

My datatable can do searching, filtering and I need to pass custom where clause on my controller.

Can you show us how to pass custom where statements?

TedAvery commented 8 years ago

@dodeagung @3s2ng If you want the where clause to change dynamically based on input, you can check the input within the model itself and it will work fine. Here is how I am doing it.

public function whereClauseArray(){
    $conditions = array();
    if ($this->input->post('year_start')) {
      $conditions['year_start >='] = $this->input->post('year_start');
    }
    if ($this->input->post('year_end')) {
      $conditions['year_end <='] = $this->input->post('year_end');
    }
    if ($this->input->post('skills')) {
      $skills = $this->input->post('skills');
      foreach ($skills as &$s) {
        $s = 'skills LIKE '.$this->db->escape('%'.$s.'%');
      }
      $conditions['('.implode($skills, ' AND ').')'] = NULL;
    }
    return $conditions;
  }
zepernick commented 8 years ago

@TedAvery Thank you for jumping in and helping out! You are exactly right.

sivapc commented 7 years ago

@TedAvery How can we set OR condition in whereClauseArray, please let me know if you have any workaround for it.

jnar commented 6 years ago

Hi all, can anyone please help me?

@TedAvery im trying to implement a conditional similar to yours, basically the idea is to restrict city view if user is not admin:

public function whereClauseArray(){ $cond = array(); $is_admin = $this->ion_auth->is_admin(); if( !$is_admin ) { $cond['v.city'] = $this->session->city; } else { $cond = NULL; } return $cond; }

But I got a CI error:

Error Number: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE 0 IS NULL LIMIT 12' at line 2 SELECT *WHERE 0 IS NULL LIMIT 12