Skip to content

Commit f60394f

Browse files
committed
Automatically resolve CodeIgniter version and update README.md
1 parent 3ad48d8 commit f60394f

File tree

3 files changed

+41
-21
lines changed

3 files changed

+41
-21
lines changed

Diff for: README.md

+20-17
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,25 @@
22

33
DataTables server-side for CodeIgniter, supported for both CodeIgniter 3 and CodeIgniter 4.
44

5-
**Note:** This library only handle the server-side part, you still needs to configure the client side like jQuery, DataTables library and including the styles. Don't worry, we already give the examples below.
5+
**Note:** This library only handle the server-side part, you will still need to set up the client-side components, such as jQuery, the DataTables library, and the necessary styles. **Don't worry, we've included examples below to help you get started.**
66

77
## Requirements
88

9-
If you are using CodeIgniter, let's go! You don't needs any extra requirements.
9+
No additional requirements are needed if you are already using CodeIgniter. Simply integrate this library into your existing project.
1010

1111
## Installation
1212

13-
You just need to use composer and everything is done.
13+
To install the library, use Composer. This command will handle the installation process for you:
1414

1515
```sh
1616
composer require ngekoding/codeigniter-datatables
1717
```
1818

1919
## Usage
2020

21-
Here is the basic example to use this library, you are freely make any changes for the client side, like defining searchable column, orderable column, etc...
21+
Below is a basic example of how to use this library. Feel free to customize the client-side configuration, such as defining searchable columns, orderable columns, and other DataTables options.
22+
23+
The usage of this library is similar for both CodeIgniter 3 and CodeIgniter 4. **The primary difference is in how you create the query builder.** Below are examples for both versions.
2224

2325
### CodeIgniter 3 Example
2426

@@ -27,16 +29,13 @@ Here is the basic example to use this library, you are freely make any changes f
2729

2830
// Here we will select all fields from posts table
2931
// and make a join with categories table
30-
// Please note: we don't need to call ->get() here
32+
// IMPORTANT! We don't need to call ->get() here
3133
$queryBuilder = $this->db->select('p.*, c.name category')
3234
->from('posts p')
3335
->join('categories c', 'c.id=p.category_id');
3436

35-
/**
36-
* The first parameter is the query builder instance
37-
* and the second is the codeigniter version (3 or 4)
38-
*/
39-
$datatables = new Ngekoding\CodeIgniterDataTables\DataTables($queryBuilder, '3');
37+
// The library will automatically detect the CodeIgniter version you are using
38+
$datatables = new Ngekoding\CodeIgniterDataTables\DataTables($queryBuilder);
4039
$datatables->generate(); // done
4140
```
4241

@@ -50,7 +49,8 @@ $queryBuilder = $db->from('posts p')
5049
->select('p.*, c.name category')
5150
->join('categories c', 'c.id=p.category_id');
5251

53-
$datatables = new Ngekoding\CodeIgniterDataTables\DataTables($queryBuilder, '4');
52+
// The library will automatically detect the CodeIgniter version you are using
53+
$datatables = new Ngekoding\CodeIgniterDataTables\DataTables($queryBuilder);
5454
$datatables->generate(); // done
5555
```
5656

@@ -123,9 +123,8 @@ Some basic functionalities already available, here is the full settings you can
123123

124124
### Use class for spesify the CodeIgniter version
125125
```php
126-
// General, use the second param to define the version
127-
// The default is 4
128-
$datatables = new Ngekoding\CodeIgniterDataTables\DataTables($queryBuilder, '3');
126+
// General, use the second param to define the version (3 or 4)
127+
$datatables = new Ngekoding\CodeIgniterDataTables\DataTables($queryBuilder, 3);
129128

130129
// CodeIgniter 3
131130
$datatables = new Ngekoding\CodeIgniterDataTables\DataTablesCodeIgniter3($queryBuilder);
@@ -177,15 +176,17 @@ $datatables->addColumnAliases([
177176
$datatables->addSequenceNumber();
178177
$datatables->addSequenceNumber('rowNumber'); // It will be rowNumber
179178

180-
// Don't forget ot call generate to get the results
179+
// Don't forget to call generate to get the results
181180
$datatables->generate();
182181
```
183182

184-
## Another Example
183+
## Complete Example
185184

186185
I already use this library to the existing project with completed CRUD operations, you can found it [here](https://github.com/ngekoding/ci-crud).
187186

188-
Please look at these files:
187+
<details>
188+
<summary>Please look at these files:</summary>
189+
189190
- application/composer.json
190191
- application/controllers/Post.php
191192
- application/models/M_post.php
@@ -194,3 +195,5 @@ Please look at these files:
194195
- application/views/posts/index-datatables-array.php
195196
- application/helpers/api_helper.php
196197
- assets/js/custom.js
198+
199+
</details>

Diff for: src/DataTables.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@ class DataTables
3232
/**
3333
* The class constuctor
3434
* @param $queryBuilder
35-
* @param string|int $ciVersion The codeIgniter version to use
35+
* @param string|int|null $ciVersion The codeIgniter version to use
3636
*/
37-
public function __construct($queryBuilder, $ciVersion = '4')
37+
public function __construct($queryBuilder, $ciVersion = NULL)
3838
{
39-
$this->ciVersion = $ciVersion;
40-
$this->config = new Config($ciVersion);
39+
$this->ciVersion = Helper::resolveCodeIgniterVersion($ciVersion);
40+
$this->config = new Config($this->ciVersion);
4141
$this->request = Request::createFromGlobals();
4242
$this->queryBuilder = $queryBuilder;
4343

Diff for: src/Helper.php

+17
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,21 @@ public static function getFieldNames($queryBuilder, $config)
4848
->{$config->get('get')}()
4949
->{$config->get('getFieldNames')}();
5050
}
51+
52+
/**
53+
* Resolve CodeIgniter version
54+
*
55+
* @param string|int|null $ciVersion
56+
* @return int
57+
*/
58+
public static function resolveCodeIgniterVersion($ciVersion)
59+
{
60+
if ( ! in_array($ciVersion, [3, 4])) {
61+
if (class_exists(\CodeIgniter\Database\BaseBuilder::class)) {
62+
return 4;
63+
}
64+
return 3;
65+
}
66+
return (int) $ciVersion;
67+
}
5168
}

0 commit comments

Comments
 (0)