CodeIgniter Forums
Simple database CRUD for Codeigniter - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=22)
+--- Thread: Simple database CRUD for Codeigniter (/showthread.php?tid=54971)



Simple database CRUD for Codeigniter - El Forum - 10-03-2012

[eluser]keevitaja[/eluser]
I wrote a simple database abstraction layer to work with database tables.

Simple Crud @ GitHub

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

Code:
class Example_model extends Simple_crud {}

Usage examples

Table: example
Fields: id, cats, dogs
Model: Example_model

Code:
// Insert a row:

$pets = new Example_model;
$pets->cats = 'Jerry';
$pets->dogs = 'Spike';
$pets->create(); // on success returns insert_id()

Code:
// Fetch a row:

$pets = Example_model::get($id); // where $id is the primary key
// or
$pets = Example_model::get(array('dogs' => 'Spike'));

Code:
// Update a row:

$pets = Example_model::get($id);
$pets->dogs = 'Harry';
$pets->update();

Code:
// Delete a row:

$pets = Example_model::get($id);
$pets->delete();
// or
$this->example_model->delete($id); // where $id id the primary key

Code:
// 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!



Simple database CRUD for Codeigniter - El Forum - 10-15-2012

[eluser]elite-board[/eluser]
very cool, was looking for something just like this Smile

thanks.