Welcome Guest, Not a member yet? Register   Sign In
Finally, I have conquered CRUD!
#1

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

[eluser]slowgary[/eluser]
I'm curious to know what the form looks like. Other than that, does it use CI validation? Is this production ready and would you actually use it for clients? And finally, will this really suffice for ALL occasions?

Thanks for playing.
#3

[eluser]gigas10[/eluser]
Ever heard of ORM? Too bad CI doesn't implement ORM, it's pretty nice. Also, distribute you're code so the rest of us can take a looksie.
#4

[eluser]@li[/eluser]
[quote author="slowgary" date="1247216617"]I'm curious to know what the form looks like. Other than that, does it use CI validation? Is this production ready and would you actually use it for clients? And finally, will this really suffice for ALL occasions?

Thanks for playing.[/quote]

Well, the forms can look like however you design them. I'm attaching screenshots of a page I built with the following code, which is a bit long, but the form had a LOT of fields:

Code:
<?php
require_once(APPPATH.'/crud/Crud.php');
class Owners extends CrudController
{
    function Owners()
    {
        parent::Controller();

        $this->entityName='Site Owner';
        $this->model='owner';
        $this->url='admin/owners';

        $this->setDefaults();

        $this->addGridField('land_owner');
        $this->addGridField('contract');
        //3rd argument tells it to format the values of this column as dates.
        //This changes them from YYYY-MM-DD to 13th August, 3009, etc
        $this->addGridField('contract_date','','date');
        $this->addGridField('last_payment','Last rent payment');
        $this->addGridField('last_payment_period');
        //3rd argument tells it to format the values as money.
        //Results can be seen in crud1.jpg
        $this->addGridField('gst','GST','money');
        $this->addGridField('total_paid','','money');
        $this->addGridField('to_pay','','money');
        $this->addGridField('paid_amt','Paid Amount','money');
        $this->addGridField('side_or_site');
        $this->addGridField('payment_status');

        $this->addTextarea('real_property_desc', '', '', '', '', 7, 50);
        $this->addTextbox('land_owner');
        $this->addTextbox('contract');
        $this->addDate('contract_date', '', '','Contract due date');
        $this->addTextbox('work_phone');
        $this->addTextbox('home_phone');
        $this->addTextbox('mobile');
        $this->addTextbox('fax');
        $this->addTextbox('email', '', '', 'valid_email');
        $this->addSelect('last_payment', $this->_lastPaymentOpts(), '', '', '', 'is_numeric', 'Last rent payment');
        $this->addTextbox('last_payment_period', '', '', '', 'Last rent payment period');
        $this->addTextbox('gst', '', '', 'numeric', 'GST', 4,'','<b>$</b> ');
        $this->addTextbox('total_paid', '', '', 'numeric', '', 4,'','<b>$</b> ');
        $this->addTextbox('to_pay', '', '', 'numeric', '', 4,'','<b>$</b> ');
        $this->addTextbox('paid_amt', '', '', 'numeric', 'Paid amount', 4,'','<b>$</b> ');
        $this->addTextbox('side_or_site');
        $this->addSelect('payment_status', array('Paid'=>'Paid','Unpaid'=>'To be paid'));
        $this->addDate('paid_upto');
        $this->addTextbox('invoice');
        $this->addTextbox('cheque');
        $this->addTextbox('bsb');
        $this->addTextbox('acct_num', '', '', '', 'Account number');
        $this->addTextbox('bank');
        $this->addTextarea('comments', '', '', '', '', 7, 50);
        
    }


    function _lastPaymentOpts()
    {
        $opts=array();
        for ($x=0; $x<=20; $x++)
        {
            $opts[$x]=$x;
        }
        return $opts;
    }

}
?&gt;

Notice how I only pass it the name of the field, e.g payment_date, and it automatically capitalizes it, removes underscores, etc on the actual forms and on the grid, showing 'Payment date' there.

Its definitely production ready though I need to add a class for producing radios (the project i was working on when I made this needed only textboxes, textareas, selects, and checkboxes).

Yes, it does use CI validation.
#5

[eluser]Raf31[/eluser]
Well that seems a pretty good job, very interesting indeed, this is the kind of thing I have been looking for CI for ages! Would you agree to post the source files here so that I can test it on one of my projects?
#6

[eluser]ferno[/eluser]
[quote author="gigas10" date="1247221581"]Ever heard of ORM? Too bad CI doesn't implement ORM, it's pretty nice. Also, distribute you're code so the rest of us can take a looksie.[/quote]

There are a few ORM libraries for CI, check out:

DataMapper (what I've recently been using) as well as
DataMapper OverZealous Edition
#7

[eluser]Raf31[/eluser]
@li, any news from you project? I would be really interested in using it for a personnal project since it seems qui powerful.
#8

[eluser]Alejus[/eluser]
Hi @li
Is your code available to see?
I need to do the things you said, a lot of copy and paste operations, for a lot of fields and tables.
I´m looking for some apps for make the job more easy.

Thank a lot.
God bless to you.

Alejus




Theme © iAndrew 2016 - Forum software by © MyBB