I wrote a simple database abstraction layer to work with database tables.
PHP 5.3 required
Simple CRUD is very simple (it really cannot be more simpler) “database abstraction layer”
for CodeIgniter, which gives you some additional benefits.
Setup
To create a new model extend it from Simple_crud instead of CI_Model.
Both Simple_crud and your model must be loaded in CI way.
Name your models <Table_name>_model. If you do not like “_model” in all
model names, just remove preg_replace from __construct() method. If table
name is example, then model would look
class Example_model extends Simple_crud {}
Usage examples
Table: example
Fields: id, cats, dogs
Model: Example_model
// Insert a row:
$pets = new Example_model;
$pets->cats = 'Jerry';
$pets->dogs = 'Spike';
$pets->create(); // on success returns insert_id()
// Fetch a row:
$pets = Example_model::get($id); // where $id is the primary key
// or
$pets = Example_model::get(array('dogs' => 'Spike'));
// Update a row:
$pets = Example_model::get($id);
$pets->dogs = 'Harry';
$pets->update();
// Delete a row:
$pets = Example_model::get($id);
$pets->delete();
// or
$this->example_model->delete($id); // where $id id the primary key
// All together: create, retrieve, update and delete. lol
$pets = new Example_model;
$pets->cats = 'cat';
$pets->dogs = 'dog';
$pets = Example_model::get($pets->create()); // new row created and fetched. wtf
$pets->cats = 'jerry';
$pets->update(); // row updated
$pets->delete(); // row deleted
// In just 7 lines we created new row, updated and deleted it!
In Example_model you can add your own custom methods as it was the
regular CodeIgniter model.
Please feel free to use it any way you like!
