Skip to content

Commit 10823b1

Browse files
committed
Add option to define column alias/aliases to prevent column ambiguous when using SELECT JOIN
Fix for issue #2
1 parent 267de27 commit 10823b1

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

README.md

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
# CodeIgniter DataTables
22

3-
DataTables server-side for CodeIgniter, supported both for CodeIgniter 3 and CodeIgniter 4.
3+
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.
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.
66

77
## Requirements
88

9-
If you are using CodeIgniter, you can use this library with no extra requirement.
9+
If you are using CodeIgniter, let's go! You don't needs any extra requirements.
1010

1111
## Installation
1212

@@ -160,6 +160,17 @@ $datatables->addColumn('action', function($row) {
160160
return '<a href="url/to/delete/post/'.$row->id.'">Delete</a>';
161161
});
162162

163+
// Add column alias
164+
// It is very useful when we use SELECT JOIN to prevent column ambiguous
165+
$datatables->addColumnAlias('p.id', 'id');
166+
167+
// Add column aliases
168+
// Same as the addColumnAlias, but for multiple alias at once
169+
$datatables->addColumnAliases([
170+
'p.id' => 'id',
171+
'c.name' => 'category'
172+
]);
173+
163174
// Add squence number
164175
// The default key is `sequenceNumber`
165176
// You can change it with give the param
@@ -180,5 +191,6 @@ Please look at these files:
180191
- application/models/M_post.php
181192
- application/views/template.php
182193
- application/views/posts/index-datatables.php
194+
- application/views/posts/index-datatables-array.php
183195
- application/helpers/api_helper.php
184196
- assets/js/custom.js

src/DataTables.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,39 @@ public function addColumn($key, $callback)
8181
return $this;
8282
}
8383

84+
/**
85+
* Add column alias
86+
* Very useful when using SELECT JOIN to prevent column ambiguous
87+
* @param string $key The key of the column name (e.g. including the table name: posts.id or p.id)
88+
* @param string $alias The column alias
89+
*
90+
* @return $this
91+
*/
92+
public function addColumnAlias($key, $alias)
93+
{
94+
$this->columnAliases[$alias] = $key;
95+
return $this;
96+
}
97+
98+
/**
99+
* Add multiple column alias
100+
* @param array $aliases The column aliases with key => value format
101+
*
102+
* @return $this
103+
*/
104+
public function addColumnAliases($aliases)
105+
{
106+
if ( ! is_array($aliases)) {
107+
throw new \Exception('The $aliases parameter must be an array.');
108+
}
109+
110+
foreach ($aliases as $key => $alias) {
111+
$this->addColumnAlias($key, $alias);
112+
}
113+
114+
return $this;
115+
}
116+
84117
/**
85118
* Only return the column as defined
86119
* @param string|array $columns The columns to only will be returned

0 commit comments

Comments
 (0)