Welcome Guest, Not a member yet? Register   Sign In
Simple database CRUD for Codeigniter
#1

[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!
#2

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

thanks.




Theme © iAndrew 2016 - Forum software by © MyBB