CRUD has 2 purposes. The one is, to let the developers focus on the DB query variables and targeted values, rather than the repetitive "Query Builder Class" syntax.
$this->db->where('foo');
$this->db->order_by('bar', 'asc');
$this->db->order_by('dee', 'desc');
$this->db->limit(30,0);
$query = $this->db->get();
if ($query) : $result = json_encode($query->result()); endif;
With CRUD loaded, the above is equivalent to:
$result = $this->crud->r('foo',
array(
'order' => array('bar' => 'asc', 'dee' => 'desc'),
'limit' => array(30,0),
),
'json');
The other purpose is, to give extra functionality to the developers who works under the LAMP environment and the expected needs they sometimes need to meet. Like, storing and retrieving the human readable names of the table fields.
Please note that this model assumes the DB is MySQL.
- Simply put
Crud.php
into your Codeigniter application's models directory and load it to use. - If you're familiar with Git, clone and make use of this entire repo as you please.
-
c($where, $what, $how = null)
Create.$where
(str) Where to insert. A table name.$what
(arr) What to insert into the table. An associative array of the fields and the content.$how
(str) How to get the result of the query. Either'json'
ornull
.
-
r($what, $where = null, $how = null, $who = false)
Read.$what
(str|arr) Where to start reading. Either:- a string of table name, to do all-fields query, or
- an associative array, to do field-specific query
// Crud.php $this->delimiter = ', '; // this is the default, set in the construct function // Your_controller.php array( 'table' => 'users', 'fields' => 'username, nickname, gender')
$where
(arr) TheWHERE
information to read the table. An associative array of eitherorder
,limit
or the existing field name.order
(arr) An associative array with one key specifying the target field and its value valid to SQL ORDER syntax, such asrandom
,asc
,desc
.limit
(arr) A simple array of 2 numbers (LIMIT
andOFFSET
)- if the key is neither
order
norlimit
, CRUD regards it as the field name and its value as the rest part of SQL WHERE syntax. For example:array('column_user_age' => '> 19')
$how
(str) How to get the result of the query. Either'json'
,'array'
(a perfect array) ornull
(array of row objects).$who
(mixed) Who is going to read it. Utilizingh()
method. Either'human'
(ortrue
) for human readable names only,'robot'
(orfalse
) for the code-friendly field names only or'world'
for both.
-
u($where, $when, $what, $how = null)
Update.$where
(str) Where to update the row. A table name.$when
(arr) When to insert. A simple array of 2 values about the target row. The first element is field name and the last element is the unique value.- For example:
array('uid', 49)
- For example:
$what
(arr) What to update into the table. An associative array of the target fields and the new contents.$how
(str) Exact same ofc($how)
.
-
d($where, $what, $how = null)
Delete.$where
(str) Where to delete a row. A table name.$what
(arr) What row to delete. A simple array of 2 values about the target row. The first element is field name and the last element is the unique value. For example:array('uid', 2)
$how
(str) Exact same ofc($how)
.
-
s($where, $when, $what, $how = null)
Set, that is equivalent to:u($where, $when, $what, $how)
, ifr($where, $when)
gives result of one row.c($where, $what, $how)
, otherwise.
-
h($where, $how = false)
Humanize (or, by retrieving the field comments) the field of the table.$where
(str) A table name to humanize.$how
(mixed) Exact same withc($how)
.
-
m($where, $what = null)
Return metadata of the table fields. An utility function specifically for$o()
.$where
(str) A table name to get the fields.$what
(str) A specific target field name.
-
o($where, $what, $how)
Check if the field type isint
and$how
contains the operator symbol, so that you can do something like this:$counted = $this->crud->u('posts', array('uid', 21), array('counts' => 'counts + 1'));
$where
(str) A table name that contains the regarding field.$what
(str) The field name in the table.$how
(str) The query string to test over the field.
A cheatsheet just in case you don't recall! :-)
create() === c()
read() === r()
update() === u()
delete() === d()
set() === s()
humanize_column_names() === humanize_columns() === humanize_column() === humanize() === h()
metadata() === m()
operator() === o()
- Update
$when
logic of bothu()
ands()
, so that it could run multiple column match test - Add more
WHERE
options likeLIKE %string%
- Pass the unit test of the code
- Supporting more database than MySQL