Disclaimer: THIS REPO IS OFFICIALLY OUT OF MAINTANENCE. Please do not expect any updates, although it's only MySQL-capable, it has no idea what "grouping" is and it may have possible bugs or points of improvements. (See "To Do" for more)
CRUD has 2 key purposes.
- To let developers build queries with structured idea.
- To give extra functionalities -- converting it into JSON, utilizing comments in tables, etc.
Suppose you need a JSON of 30 foo
records ordered by bar
and dee
, which is fairly the most common daily use case of CodeIgniter models.
$this->db->where('foo');
$this->db->order_by('bar', 'asc');
$this->db->order_by('dee', 'desc');
$this->db->limit(30, 0);
$foos = $this->db->get();
$json = json_encode($foos->result());
With CRUD loaded, you do that like this.
$json = $this->crud->r('foo', [
'order' => ['bar' => 'asc', 'dee' => 'desc'],
'limit' => [30, 0]
], 'json');
It would help you out when you:
- have 100+ tables that have no complicated relationships, or
- have to define 100+ model methods to get these fields or that fields, and
- have 0 of "heavy queries"
So choose at your own risk and enjoy.
- Load
Crud.php
as you want it -- third party package, CI library or one of the models - Clone or fork this repo and everybody gangsta
-
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),'row'
(when you ) 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()
I probably do. Now the repo is applied of Git Flow, FYI.
Since CRUD has not any good idea of DB drivers or any support for mid-level SQL needs like JOIN, GROUP, COUNT or else, this approach should be replaced by something enhanced. To be announced.
MIT