Skip to content
Derek Jones edited this page Jul 5, 2012 · 12 revisions

Category:Libraries::CRUD

Acicrud (Automatic CodeIgniter Create Replace Update Delete)is a CRUD library that provides a ready-to-use and fully functional Model for all of your application tables.

The 2009-12-30 release have been fully tested and is currently used in production environment by more than 15 web applications over the Web.

Please se the [url=http://www.kromack.com/acicrud-english/]Acicrud project's home in english[/url] or in [url=http://www.kromack.com/acicrud/]french[/url] for downloading, API documentation, tutorials and support.

Please note that currently, Acicrud can fully (and only) manage a table for which at least the first normal form is applicable.

I know this library is less powerfull that an ORM library, but Acicrud can be very suitable for rapid developpement, so I'm sharing !

Some exemples are available below :

Creating a Model

<?php

class Exemple extends Acicrud {

    //CONSTRUCTOR   
   
    public function __construct()
    {
        parent::__construct('table_name');
    }

    //CUSTOM METHODS
     
}

?>

Reading a row

<?php
 
$this->load->model('exemple');
 
try {
    $o= $this->exemple->read(1); //Primary key id
} catch(Exception $e) {
    echo "Wrong ID";
}
 
var_dump($o);
 
//Produces
/*
object(stdClass) (4) {
  ["idExemple"]=>
  string(2) "1"
  ["title"]=>
  string(8) "title..."
  ["description"]=>
  string(15) "description..."
  ["date"]=>
  string(19) "2009-04-30 21:00:00"
}
*/
 
?>

Reading 10 rows

<?php
 
$this->load->model('exemple');
 
try {
 
    //Retrieve an array of objects, this array can be passed to a view for displaying data
    foreach($this->exemple->getAll(10, array('date' => 'DESC') as $row) {
 
        var_dump($row); 
 
        //Produces
        /*
        object(stdClass) (4) {
          ["idExemple"]=>
          string(2) "the id"
          ["title"]=>
          string(8) "the title"
          ["description"]=>
          string(15) "the description"
          ["date"]=>
          string(19) "the date"
        }
        */
    }
 
} catch(Exception $e) {
    echo $e->getMessage();
}
 
?>

Creating a row (with form_validation)

<?php
 
class Form extends Controller {
 
    public function index()
    {    
        //Form_validation stuff
        $this->load->library('form_validation');
 
        $this->form_validation->set_rules('title', 'Title', 'required');
        $this->form_validation->set_rules('description', 'Description', 'required');
 
        if ($this->form_validation->run() == FALSE)
        {
            $this->load->view('myform');
        }
        else
        {
            //Validation success
            $this->load->model('exemple');
 
            $o->title = $this->input->post('title');
            $o->description = $this->input->post('description');
            $o->date = date("Y-m-d H:i:s");
 
            //Creating the row in the database
            $this->exemple->create($o);
        }
    }
    }
?>

Deleting a row

<?php
 
class Element extends Controller {
 
    public function Element()
    {
        parent::Controller();    
        $this->load->model('elementModel');
    }
 
    public function deleteElement($id)
    {
        try {
 
            //Try to read the row identified by $id
            $element = $this->elementModel->read($id);
 
            //Delete the database row
            $this->elementModel->delete($element->id);
 
        } catch(Exception $e) {
 
            die('Bad id');
 
        }
    }
 
}
?>
Clone this wiki locally