If you like this library or find it useful, please help a programer in need! Please donate so I can continue creating libraries.
Download the CRUD Model for Codeigniter: here
Update 25 May 2011 - for Codeigniter 2.0
If you are using Codeigniter 2.0.x this model does not work (This model is for CI 1.7.3). There is a NEW version of CRUD for Codeigniter 2.0.x and it is locate here.
A CRUD Library for Codeingiter 1.7.3
This is a CRUD (Create, Retrieve, Update, and Delete) Model. One of the powerful things about this model is you can simply pass an ARRAY of keyed data to the function and it will break the arrays up and use them intelligently. For example how many times have you written a database query and realized you needed to add a new condition to it, or organize it differently? To do so with this model, all you do is add a new element to an array and you are back in business. The array can even indicate other operators other than = such as >, <, !=, >= , <=. This entire library is built on Codeigniters Active Record Class, this allows you to use extra features of that class with your CRUD queries. (For example you can use Codeigniter Active Record select statements, among other things.)
So if I had a database and I wanted to query for results I would do this:
// Build the criteria $criteria = array('id >=' => 1, 'title' => $title_string, 'published' => 1); // Build the sort array $sort = array( 'title' => 'asc', 'date' => 'desc'); // Call the Crud function $query = $this->crud->retrieve($criteria, 5, 0, $sort); // OR the arrays can be specified INLINE so you do not have to use the extra variables. $query = $this->crud->retrieve(array('id >=' => 1, 'title' => $title_string, 'published' => 1), 5, 0, array('title' => 'asc', 'date' => 'desc')); // Both of these statements are identical... // Now deal with $query as you would any Codeigniter Active Record Value....
This queries the database for anything that has an ID greater than or equal to 1, with a title matching the $title string, that has a "published" value of TRUE (1). It grabs the first 5 entries and sorts them alphabetically by title, and then by date. The first array determines the criteria, and you can easily change your criteria by modifying the array. The second element is your limit (how many to grab), and the third element is the offset. The last array is the SORT array, it goes in order of keys, and can be designated 'asc', 'desc', or 'random'. And you can add as many conditions to the array as you like to make remarkably complicated requests quite easily.
This library is a "model" to be included into a base Codeigniter install. This does not solve all of your database needs. However it should solve about 80% of them (particularly the simple ones.)
How to Use / Install
There are 2 ways to use this.
- Copy the model to your model folder and start using it.
- Copy the model, controller, and view into the proper folders, and use the example given.
If you want to use the example code I gave here is what needs to happen.
- You need a web server that can run Codeignitner, and have access to a Database. Setting this up is beyond the scope of this doc.
- You need to setup Codeigniter so that it works on the server and is able to connect to the database. (configuring things like the database.php file)
- You need to create a database named "simple" so that we have a "test" database to work with. ALL of the example code calls a database called "simple". (This can be changed.)
Copy the following files
- /system/application/controller/example.php to your controller folder
- /system/application/model/crud.php to your model folder
- /system/application/view/index.php to your view folder
- Then go to your base_url()/example for me it was something like: http://localhost/index.php/example (You need to figure out what your base_url is. I can't help you there.)
- Click on the make table link at the top (only once. If you click on it more than that it will generate an error, because it is trying to generate a table that already exists... sorry i didn't fee like error checking it just for an example.)
- Generate some Entries...
Use the interface...
You can look at my sample code in "example.php" and even the crud model it self. I OVER documented so that it would be straight forward.
If you are DARING! You can just copy the crud.php to your model folder and start reading my notes in there. It should be pretty straight forward if you are familiar with PHP and Codeigniter at all.
Documentation and Help
The source code is abundantly documented. You can look through it. You can post questions on my site: http://taggedzi.com via the comment or contact pages.
Documentation : Usage
In order to use this model, you must have a working database connection already configured in Codeigniter. Then you must call the model like this:
After you call it you MUST declare the table you plan to plan to manipulate. With this line,
You can call that function as many times as you need to change tables. But if you do not have a table set, the library will not work, and it will generate a log entry explaining why. This model has been built with logging in mind so if you are getting errors, or unexpected results. Make sure you have logging turned on AND your Codeigniter install can write the logs. The module will give copious amounts of information out if logging is set to "debug". If you are having problems of any kind I would look there first.
After you have loaded the model, and declared your table, then you can use the following functions as noted.
Documentation: Function List
/** * function use_table($table_name) * * This function is required in order to select a table Before anything else. * * @param string $table_name The name of the table to select * for crud operations * @return boolean True if successful / False on failure */
/** * function create ($data_in) * * This function creates an entry based on $data_in * * @param array $data_in A keyed array of criteria key = field name, * value = value * @return boolean True if successful / False if not */
retrieve($criteria, $limit, $offest, $order)
/** * function retrieve ($criteria, $limit, $offest, $order) * * 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) * @return mixed Return Object of results on success, Boolean False on failure */
update($criteria, $data_in, $limit, $offset, $order)
/** * function update ($criteria, $data_in) * * 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) * @return mixed Return Object of resutls on success, Boolean False on failure * */
delete($criteria, $limit, $offset, $order)
/** * function delete($criteria, $limit, $offset, $order) * * 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) * @return mixed Return Object of resutls on success, Boolean False on failure */
/** * function count() * * This function simply counts ALL entries in the selected DB. * @return Mixed Return Integer of resutls on success, Boolean False on failure */
/** * function count_results($criteria) * * 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. * @return Mixed Return Integer of resutls on success, Boolean False on failure */
/** * function is_entry_unique($criteria) * * 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. * @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) */
I am copyrighting the code. I using the same type of license as the Codeigniter.
You are permitted to use, copy, modify, and distribute the Software and its documentation, with or without modification, for any purpose, provided that the following conditions are met:
1. A copy of this license agreement must be included with the distribution.
2. Redistributions of source code must retain the above copyright notice in all source code files.
3. Redistributions in binary form must reproduce the above copyright notice in the documentation and/or other materials provided with the distribution.
4. Any files that have been modified must carry notices stating the nature of the change and the names of those who changed them.
You agree to indemnify and hold harmless the authors of the Software and any contributors for any direct, indirect, incidental, or consequential third-party claims, actions or suits, as well as any related expenses, liabilities, damages, settlements or fees arising from your use or misuse of the Software, or a violation of any terms of this license.
Disclaimer of Warranty
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, WARRANTIES OF QUALITY, PERFORMANCE, NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
Limitations of Liability
YOU ASSUME ALL RISK ASSOCIATED WITH THE INSTALLATION AND USE OF THE SOFTWARE. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS OF THE SOFTWARE BE LIABLE FOR CLAIMS, DAMAGES OR OTHER LIABILITY ARISING FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE. LICENSE HOLDERS ARE SOLELY RESPONSIBLE FOR DETERMINING THE APPROPRIATENESS OF USE AND ASSUME ALL RISKS ASSOCIATED WITH ITS USE, INCLUDING BUT NOT LIMITED TO THE RISKS OF PROGRAM ERRORS, DAMAGE TO EQUIPMENT, LOSS OF DATA OR SOFTWARE PROGRAMS, OR UNAVAILABILITY OR INTERRUPTION OF OPERATIONS.
- Tags: CRUD, Create, Retrieve, Update, Delete, MySQL, Database, CodeIgniter
- Description: This is a generic CRUD Library for CodeIgniter.