diff --git a/Controller/Component/DataTableComponent.php b/Controller/Component/DataTableComponent.php index a19d2f0..5a66bac 100644 --- a/Controller/Component/DataTableComponent.php +++ b/Controller/Component/DataTableComponent.php @@ -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; } @@ -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'].'%' diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..147801d --- /dev/null +++ b/composer.json @@ -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": "cnizzdotcom@cnizzdotcom.com" + } + ] +}