CRUD for Codeigniter 2

CRUD for Codeigniter 2.0

CRUD for Codeigniter 2.0

I recently (yesterday) decided to dust off my CRUD model, and discovered it did not work for Codeigniter 2.0.   So, I have completely overhauled it and even built a new demo to show people how to use it.  I have installed this and tested it personally so it should work for all you cats out there.

Donate

If you like the library and find it useful please consider donating.  Thanks to those of you who have already contributed.

 

Download

Download CRUD (version 7) for Codeigniter 2.0

It is presently at version 7.  (25 May 2011 @ 12:18AM EST).  I have just recently upgraded all my project to version management via Bazaar, including this project. So future updates will be listed by version number.

How to install:

If you just want to install the model

  1. Download the zip
  2. Extract the zip file
  3. Copy the file to "application/models/crud.php" of your Codeigniter install.
  4. Start using CRUD

 

DEMO / CRUD Example:

CRUD example (version 7) for Codeigniter 2.0.

If you want an example of how to use this CRUD model this is what you want. I use EVERY function of my model in a bare bones Blog showing how to use CRUD in practical examples.  It is well documented.  Just extract the zip file into your Codeigniter application folder.  It will install a controller "crud_example" and 4 views. This demo should not be used in production environments or anyplace where there are security concerns.  It is fairly open, and it is supposed to show off how to use CRUD not how to secure a server.

To use the demo you must have your Codeigniter setup and installed on a web server with an ACTIVE database connection, that is configured in Codeigniter for this to work.  I recommend setting up the example in an EMPTY database, because it creates tables with in the database to use.

How to use:

You must load the model to use it, you load it like this:

$this->load->model('crud');

After you load it you must specify what table you want to perform operations on. You do so like this:

$this->crud->use_table('your_table_name');

You can change tables as many times as you like, but every time you change you must use that call.

Here is a list of functions with their documentation:

 

Create:

/**
* create
*
* This function creates an entry based on $data_in
*
* @param array $data_in A keyed array of criteria key = field name, value = value
* @access public
* @return boolean True if successful / False if not
*/
public function create($data_in = '') 

"data_in" is a keyed array showing the data you want to add. For example it could look like this:

$data_in = array ( 'id' => 6, 'title' => 'My Title', 'date' => '25 May 2011');
$this->crud->create($data_in);

This would create an entry in the db with the provided data, BUT if a field is required by the database that is not provided it will fail. (Make sure you have all the required fields.)

Retrieve

/**
* retrieve
*
* This function retrieves a series of db entries based on criteria.
*
* @param array $criteria A keyed array of criteria key = field name, value = value, key may also contain comparators (=, !=, >, etc..)
* @param int $limit The max number of entries to grab (0 = no limit)
* @param int $offset What record number to start grabbing (useful for pagination)
* @param array $order A keyed array of "order commands" telling how to sort key = field name, value = direction (asc, desc, random)
* @access public
* @return mixed Return Object of results on success, Boolean False on failure
*/
public function retrieve($criteria = '', $limit = 0, $offset = 0, $order = '') 

Criteria is a keyed array (just like for add) where each key corresponds to a database field, and each value corresponds to what you are looking for. For example if you wanted to get an entry who's ID was 5, you would do this.

$criteria = array( 'id' => 5);
$query = $this->crud->retrieve($criteria);

This would get EVERY field who's ID matched "5".  (Ideally ID's should be unique so in this case it should only return one.) You treat the results it produces IDENTICALLY to a Codeigniter ACTIVE QUERY result.  (This uses CI's Active query to do its work... so the results will be identical.)

Update

/**
* update
*
* This function updates entries that meet the listed criteria with the input data.
*
* @param array $criteria A keyed array with the criteria for selecting what entries to edit.
* @param array $data_in A keyed array with the data to insert (key = db field name, value = value to insert)
* @param int $limit The max number of entries to grab (0 = no limit)
* @param int $offset What record number to start grabbing (useful for pagination)
* @param array $order A keyed array of "order commands" telling how to sort key = field name, value = direction (asc, desc, random)
* @access public
* @return mixed Return Object of results on success, Boolean False on failure
*/
public function update($criteria = '', $data_in = '', $limit = 0, $offset = 0, $order = '') 

"$criteria" is just like the criteria used in the retrieve method, it just tells the crud model what needs to get updated. (It can update MULTIPLE entries at once so be careful.)

"$data_in" is just like the array used in the create method.  It is an associated array where the keys are the db field names and the values are what gets inserted in the database.

Delete

/**
* delete
*
* This function deletes entries based on criteria input.
*
* @param array $criteria A keyed array with the critera for selecting what entries to delete.
* @param int $limit The max number of entries to grab (0 = no limit)
* @param int $offset What record number to start grabbing (useful for pagination)
* @param array $order A keyed array of "order commands" telling how to sort key = field name, value = direction (asc, desc, random)
* @access public
* @return mixed Return Object of resutls on success, Boolean False on failure
*/
public function delete($criteria = '', $limit = 0, $offset = 0, $order = '') 

Just like the other functions criteria is an array indicating "what" gets deleted.  It is completely possible to delete a lot of entries at once, so be careful and I strongly recommend the usage of the "limit" as well.

count_all

/**
* function count_all()
*
* This function simply counts ALL entries in the selected DB.
*
* @access public
* @return Mixed Return Integer of resutls on success, Boolean False on failure
*/
public function count_all() 

This function is only here for people who want to do EVERYTHING with CRUD... you can just as easily use CI for this one... but I included it as it was very simple.

count_results

/**
 * count_results
 *
 * This function simply counts ALL entries in the selected DB with a given criteria.
 *
 * @param   array   $criteria    A keyed array with the critera for selecting what entries to edit.
 * @access  public
 * @return  Mixed   Return Integer of resutls on success, Boolean False on failure
 */
public function count_results($criteria) 

This uses criteria just like the other functions (retrieve, update, delete) to determine what you want to count.  Just like the other functions you can include as many fields as you want.

is_entry_unique

/**
* is_entry_unique
*
* This function checks to see if an entry exists in the database matching the given criteria.
*
* @param array $criteria A keyed array with the critera for selecting what entries to edit.
* @access public
* @return boolean Return Boolean TRUE if no match was found (aka it is unique). FALSE if a match is found (aka it is not unique)
*/
public function is_entry_unique($criteria = '') 

This function tests to see if the requested criteria is unique.  This is handy.  Check the demo included for interesting usages of this method.

Page Information:
  • Tags: CRUD, Codeigniter, 2.0
  • Description: This is a CRUD model for Codeigniter 2.0.