[eluser]@li[/eluser]
I was sick of writing the same code all over again for making CRUD pages (create, read, update, delete) so I wrote a class for handling all that. The controller below is a result of that:
Code:
<?php
require_once(APPPATH.'/crud/Crud.php');
class Customers extends CrudController
{
function Customers()
{
parent::Controller();
$this->entityName='Customer';
$this->model='customer';
$this->url='admin/customers';
$this->setDefaults();
//These are the fields shown on the 'grid' page which lists all the records.
//I can also specify dates, money values, etc to be displayed differently than
//normal fields or specify a custom callback function which formats the values
$this->addGridField('business');
$this->addGridField('contact_name');
$this->addGridField('email');
//These are the fields shown on the Add & edit customer forms.
$this->addTextbox('business');
$this->addTextbox('contact_name');
$this->addTextbox('phone');
$this->addTextbox('mobile');
$this->addTextbox('email', '', '', 'valid_email');
$this->addTextarea('comments', '', '', '', '', 7, 50);
}
}
?>
The functions index(), add(), edit(), add_save(), edit_save(), validate(), and del() are all inherited from CrudController, and the view files needed for showing these forms are also generated. Pagination is also handled automatically.
P.S the model is also inherited from a base model class, so I only have to change the table name and all the functions for retrieving rows, saving, etc are inherited automatically.
Any thoughts/feedback?