Skip to content
This repository has been archived by the owner on Dec 9, 2022. It is now read-only.

Filtering on BelongsTO fields in containable #24

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 23 additions & 3 deletions Controller/Component/DataTableComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,30 @@ public function getResponse($controller = null, $model=null){
// check for WHERE statement in GET request
if(isset($httpGet) && !empty($httpGet['sSearch'])){
$conditions = $this->getWhereConditions();


$this->controller->paginate = array_merge_recursive($this->controller->paginate, array('conditions'=>$conditions));

if( !empty($this->controller->paginate['contain']) ){
$this->controller->paginate = array_merge_recursive($this->controller->paginate, array('contain'=>$conditions));
// Apply the conditions to the root model fields
$fields = !empty($this->fields) ? $this->fields : $this->controller->paginate['fields'];

$modelConditions = array();
foreach($fields as $x => $column){
// Check that the field is searchable
if( $this->controller->request->query['bSearchable_'.$x] == 'true'){
list($table, $field) = explode('.', $column);

// Check that the field is part of the root model with or without the model name appended
if (($table == $this->model->name && in_array($field, $this->controller->paginate['fields'])) || in_array($column, $this->controller->paginate['fields'])){
$modelConditions[] = $column.' LIKE "%'.$this->controller->request->query['sSearch'].'%"';
}
}
}

$this->controller->paginate = array_merge_recursive($this->controller->paginate, array('conditions'=>array("OR" => $modelConditions)));
}
else{
$this->controller->paginate = array_merge_recursive($this->controller->paginate, array('conditions'=>array('AND'=>$conditions)));
$this->controller->paginate = array_merge_recursive($this->controller->paginate, array('conditions'=>$conditions));
}
$isFiltered = true;
}
Expand Down Expand Up @@ -233,6 +251,8 @@ private function getWhereConditions(){
// only create conditions on bSearchable fields
if( $this->controller->request->query['bSearchable_'.$x] == 'true' ){

list($table, $field) = explode('.', $column);

if($this->mDataProp == true){
$conditions['OR'][] = array(
$this->controller->request->query['mDataProp_'.$x].' LIKE' => '%'.$this->controller->request->query['sSearch'].'%'
Expand Down
16 changes: 16 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"name": "cnizzdotcom/cakephp-datatable",
"description": "Provides server-side interoperability between CakePHP 2.x and jQuery DataTables plugin.",
"type": "cakephp-plugin",
"require": {
"cakephp/cakephp": "~2.0",
"php": ">=5.3"
},
"license": "MIT",
"authors": [
{
"name": "Chris Nizzardini",
"email": "[email protected]"
}
]
}