Welcome Guest, Not a member yet? Register   Sign In
dont repeat yourselve CRUD
#1

[eluser]reyntjensw[/eluser]
Hi,

I'm having this question, while creating my new app, I've noticed that lots of the model code is almost identical. Can I create my CRUD functions in a helper or where can I put them so I can use them over and over again?

Thanks
#2

[eluser]Jamie Rumbelow[/eluser]
Hi reyntjensw,

The best place to put re-usable code for the model is a parent model (MY_Model). I've created my own MY_Model solution which you can use by placing in your application/libraries directory and extending your models from MY_Model instead of Model. Get it on BitBucket.

Jamie
#3

[eluser]Buso[/eluser]
thanks for sharing! :lol:
#4

[eluser]pbarney[/eluser]
Is there documentation?
#5

[eluser]J Maxwell[/eluser]
Or use DataMapper DMZ and then a model can be as brief as:

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Page extends DataMapper {
    
    function __construct($id = null) {
        parent::__construct($id);
    }
}

That is about as DRY as you can get, imho.

EDIT

and if you specify an id when you instantiate the class, then you can get an object of the model to play with by doing something as elegant as:

$p = new Page($id);

PHP might not be as elegant as RoR, but it can come pretty close sometimes.
#6

[eluser]pickupman[/eluser]
There is also a CLI app called CrudIgniter that will create controllers/models/views for a table with all the CRUD methods.




Theme © iAndrew 2016 - Forum software by © MyBB