From da22cb67dc5295ab1ee66a4aab3228f8ae8c0df7 Mon Sep 17 00:00:00 2001 From: klarkc Date: Mon, 27 Oct 2014 12:10:27 -0300 Subject: [PATCH 1/5] Filtering on BelongsTO fields in containable --- Controller/Component/DataTableComponent.php | 26 ++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) 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'].'%' From 05131d707d4ff7864842d242aad77d938fee864e Mon Sep 17 00:00:00 2001 From: klarkc Date: Tue, 25 Aug 2015 11:56:36 -0300 Subject: [PATCH 2/5] Added composer package file --- composer.json | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 composer.json diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..9f8b88d --- /dev/null +++ b/composer.json @@ -0,0 +1,16 @@ +{ + "name": "klarkc/cakephp-datatable", + "description": "Provides server-side interoperability between CakePHP 2.x and jQuery DataTables plugin.", + "type": "cakephp-plugin", + "require": { + "cakephp/cakephp": "2.*", + "php": ">=5.3" + }, + "license": "MIT", + "authors": [ + { + "name": "Chris Nizzardini", + "email": "cnizzdotcom@cnizzdotcom.com" + } + ] +} From 94095c320d028def423636d26d39363b445885ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Walker=20Gusm=C3=A3o?= Date: Tue, 25 Aug 2015 11:57:47 -0300 Subject: [PATCH 3/5] Update composer.json --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 9f8b88d..2104b3b 100644 --- a/composer.json +++ b/composer.json @@ -1,5 +1,5 @@ { - "name": "klarkc/cakephp-datatable", + "name": "cnizzdotcom/cakephp-datatable", "description": "Provides server-side interoperability between CakePHP 2.x and jQuery DataTables plugin.", "type": "cakephp-plugin", "require": { From 42f0196893ac0a4a0d6dc7ff2816649b4ae2cde3 Mon Sep 17 00:00:00 2001 From: klarkc Date: Wed, 26 Aug 2015 11:12:06 -0300 Subject: [PATCH 4/5] Fixed dependency --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 9f8b88d..265f7ca 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description": "Provides server-side interoperability between CakePHP 2.x and jQuery DataTables plugin.", "type": "cakephp-plugin", "require": { - "cakephp/cakephp": "2.*", + "cakephp/cakephp": "~2.3", "php": ">=5.3" }, "license": "MIT", From f39d66bb82ccf821836837c4f9eacc46a8c1c575 Mon Sep 17 00:00:00 2001 From: klarkc Date: Wed, 26 Aug 2015 11:27:59 -0300 Subject: [PATCH 5/5] Dependency Fix --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 265f7ca..72018c7 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "description": "Provides server-side interoperability between CakePHP 2.x and jQuery DataTables plugin.", "type": "cakephp-plugin", "require": { - "cakephp/cakephp": "~2.3", + "cakephp/cakephp": "~2.0", "php": ">=5.3" }, "license": "MIT",