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

Commit

Permalink
Merge branch 'anyeloamt-master' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
cnizzardini committed Jun 26, 2014
2 parents 2ac512a + c0aa136 commit a87d020
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 76 deletions.
24 changes: 13 additions & 11 deletions Controller/Component/DataTableComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @author chris
* @package DataTableComponent
* @link http://www.datatables.net/release-datatables/examples/server_side/server_side.html parts of code borrowed from dataTables example
* @since version 1.1.1
* @since version 1.2.1
Copyright (c) 2013 Chris Nizzardini
Permission is hereby granted, free of charge, to any person obtaining a copy
Expand Down Expand Up @@ -216,20 +216,22 @@ private function getWhereConditions(){

$conditions = array();

if(!empty($this->fields) || !empty($this->controller->paginate['fields']) ){
$fields = !empty($this->fields) ? $this->fields : $this->controller->paginate['fields'];
}
else{
$fields = array();

if($this->mDataProp == true){
for($i=0;$i<$this->controller->request->query['iColumns'];$i++){
$fields[] = $this->controller->request->query['mDataProp_'.$i];
if(!isset($this->controller->request->query['bSearchable_'.$i]) || $this->controller->request->query['bSearchable_'.$i] == true){
$fields[] = $this->controller->request->query['mDataProp_'.$i];
}
}
}

else if(!empty($this->fields) || !empty($this->controller->paginate['fields']) ){
$fields = !empty($this->fields) ? $this->fields : $this->controller->paginate['fields'];
}

foreach($fields as $x => $column){

// only create conditions on bSearchable fields
if( $this->controller->request->query['bSearchable_'.$x] == 'true' ){
if (isset($this->controller->request->query['bSearchable_'.$x])) { // check if the parameter exists in query.

if($this->mDataProp == true){
$conditions['OR'][] = array(
Expand Down Expand Up @@ -266,7 +268,7 @@ private function getWhereConditions(){
}
}
}
}
}
}
return $conditions;
}
Expand Down Expand Up @@ -394,4 +396,4 @@ public function getTimes(){

return $times;
}
}
}
74 changes: 9 additions & 65 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,14 @@ Features
* Accepts dataTables ORDER BY requests
* Accepts dataTables WHERE conditions
* Accepts dataTables pagination
* Works LinkableBehavior and to a lesser extent Containable
* Works with LinkableBehavior and to a lesser extent Containable

Dependancies
------
* PHP 5.x
* CakePHP 2.x
* LinkableBehavior is recommended but not required

Updates
------
* Mar 14, 2014 (v1.2.0) - Added support for mDataProp
* Feb 13, 2014 (v1.1.2) - Updated to support LIMIT and OFFSET parameters in newer versions of CakePHP
* Nov 1, 2013 (v1.1.1) - Improved performance of table count by setting model->recursive = -1 on count queries
* May 25, 2013 - added full demo under test. Resolved some bugs dealing with SQL LIMITS and cleared up some error handling.
* March 28, 2013 - added initialize method. It use to be that you had to pass in a reference to the Controller and Model when calling getResponse. This is no longer required.
```php
// old way
$this->DataTable->getResponse($this->OrdersController,$this->Order);
// new way
$this->DataTable->getResponse();
```

Installation
------
Expand All @@ -55,11 +42,16 @@ The demo application is designed for CakePHP 2.3 and was built on Ubuntu 12.10 r
Documentation
------

I'm in the process of enhancing documentation and moving it out of the README.
I'm in the process of enhancing documentation and moving it out of the README and into the [wiki](https://github.com/cnizzdotcom/cakephp-datatable/wiki/_pages).

[Basic Usage](https://github.com/cnizzdotcom/cakephp-datatable/wiki/Basic-Usage-v1.2.0)
* [Release Notes and Updates](https://github.com/cnizzdotcom/cakephp-datatable/wiki/0.-Release-Notes-and-Updates)
* [Getting Started](https://github.com/cnizzdotcom/cakephp-datatable/wiki/1.-Getting-Started)
* [Basic Usage](https://github.com/cnizzdotcom/cakephp-datatable/wiki/2.-Basic-Usage-v1.2.0)
* [Model Associations with Linkable](https://github.com/cnizzdotcom/cakephp-datatable/wiki/3.-Model-Associations-with-Linkable)
* Model Associations with Containable
* [SUM, CONCAT, and other SQL Functions](https://github.com/cnizzdotcom/cakephp-datatable/wiki/5.-SUM,-CONCAT,-and-other-SQL-Functions)

Ordering and conditions supplied via DataTables work "automagically" and nothing else is needed. Deep relations using the ContainableBehavior will break the response due to the way conditions work within that behavior. To get around this it is recommended that the LinkableBehavior be used instead. See: https://github.com/dereuromark/tools/blob/master/Model/Behavior/LinkableBehavior.php
Ordering and conditions supplied via DataTables work "automagically" and nothing else is needed. Deep relations using the ContainableBehavior will break the response due to the way conditions work within that behavior. To get around this it is recommended that the [LinkableBehavior](https://github.com/dereuromark/tools/blob/master/Model/Behavior/LinkableBehavior.php) be used instead. The Component respects many of the options you can define within jQuery DataTables settings such as bSearchable and bSortable on a per field basis.


With ContainableBehavior:
Expand All @@ -75,60 +67,12 @@ With ContainableBehavior:
$this->set('_serialize','response');
```

With LinkableBehavior:
```php
$this->paginate = array(
'fields' => array('Field.A','AssocModel.B'),
'link' => array('AssocModel')
);
$this->set('response',$this->DataTable->getResponse());
$this->set('_serialize','response');
```

With CONCAT Fields. Note once a CONCAT is used we must tell the component the order the fields should be in.
```php
$this->paginate = array(
'fields' => array('Field.A','Field.B'),
'link' => array(
'AssocModel' => array(
'fields' => array('CONCAT(User.first_name," ",User.last_name) as name')
)
)
);
$this->DataTable->fields = array('Field.A','Field.B','0.name');
$this->set('response',$this->DataTable->getResponse());
$this->set('_serialize','response');
```

If you don't use Cakes built in REST code you can ofcourse just do the following to get the response data:
```php
$this->autoRender = false;
// your code here
echo json_encode($this->DataTable->getResponse());
```

The conditionsByValidate attribute should be avoided for now, but I am working on a way to make the conditions more intelligent. For now the conditions are entered in as Field.A LIKE '%$var%' OR Field.B LIKE '%$var$' etc...

The Component respects many of the options you can define within jQuery DataTables settings such as bSearchable and bSortable on a per field basis.


Using models from other controllers. Sometimes the case may be that you are in a CustomersController and you have a method within that wants to display data from another model such as an Order model. This
can be accomplished with the following parameters:
```php
$this->DataTable->getResponse(null,$this->Order);
```

Using mData

New in v1.2.0 is support for mData. When I originally wrote this component I didn't realize that dataTables supported
strings for indexes (such as aData.User.name instead of aData[1]). This functionality is off by default (for now). To
enable this parameter set your mData properties in your JS code (https://datatables.net/usage/columns) and in your
server side code set DataTable->mDataProp = true just before calling getResponse.
```php
$this->DataTable->mDataProp = true;
$this->DataTable->getResponse();
```

Licensing
------
Code is licensed under the MIT License.

0 comments on commit a87d020

Please sign in to comment.