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

[eluser]taggedzi[/eluser]
Last year I wrote a CRUD Model for CI 1.7.x and used it quite happily. I recently updated to CI 2.0 and discovered my CRUD library did not work with the update.

So, I have completely overhauled my CRUD Model and written a completely new example/demo to show how to use it. The demo is a bare bones blog. (VERY bare bones.)

This has Create, Retrieve, Update, Delete, Count All, Count Results, and Is Entry Unique functions. It is built on Codeigniters Active records, so it is completely compatible with CI's active record calls, and you can treat the results identically to the way you would normally treat CI's active record results.

There are 2 different downloads.

1. Just the model. (Direct Link to CRUD Version 7)

OR

2. The model AND a sample web app using the CRUD model showing how to use it in practical applications. (Direct link to download CRUD Version 7 WITH Example Code)

The code is clean and VERY well documented. I strongly recommend you visit the page and read the documentation in the included files BEFORE using.

You can find the model and the demo app at my website CRUD for Codeigniter 2.0

Hope it helps.
#2

[eluser]JonoB[/eluser]
Looks ok, but wht not just extend base model using MY_ ?

For example: https://bitbucket.org/jamierumbelow/code...base-model
#3

[eluser]taggedzi[/eluser]
Well, it is a good idea, but at the moment it is not possible, at least according to the Codeigniter User Guide.

http://ellislab.com/codeigniter/user-gui...aries.html

Quote:Note: At this time the Database classes can not be replaced with your own versions.

My original concept for this model (long before public release) was an extension of the database class, but I found a model worked perfectly for what I wanted, so that is what I ran with. I like the idea and if CI ever opens up the DB class to extension I may consider it. But for now, a model works perfectly for what I need, so I'll stick with it.

[User Edited: spelling correction]
#4

[eluser]InsiteFX[/eluser]
He means extend the base Model class to a MY_Model

InsiteFX
#5

[eluser]taggedzi[/eluser]
I've been thinking about it.

Pro's to Extending CI's Core Model
1. It would be loaded when CI initializes.
2. It could be extended via other models...

Con's to Extending CI's Core Model
1. It would always be there, even when I didn't want it.
2. You would still have to manually call the "use_table" method before running any queries.

The change could be easily done... but I don't know if I would always want those functions for EVERY model, unless there is something I'm missing here...

I primarily use the CRUD model to do my "simple" queries, and fall back to a more traditional model either using direct PHP/MySQL or CI's Active records, when I need to perform more complex operations like multiple joins etc...

What are the advantages that I'm not seeing by doing it that way over my current method? (This is not rhetorical...)

I could see being able to "extend" my CRUD could be handy... but on the flip side of that I have trouble thinking of a situation that would require extending it that wouldn't be far easier using a traditional PHP approach or using CI's Active records... which I would place in a separate model...

Input is welcome... Smile
#6

[eluser]JonoB[/eluser]
Maybe its just personal preference, but having all those CRUD functions automatically loaded and available for use with all my other models makes CRUD coding much faster and simpler for me. And more extensible and intuitive. Whether you are extending CI_Model or MY_Model is pretty much irrelevant if MY_Model only contains 1000 lines or so.

Oh, and you dont have to call the 'use_table' method every time...you can have MY_Model automatically work this out from the class that being called - just inflect it from the class name. Something like:

Code:
private function _fetch_table()
{
  if ($this->_table == NULL)
  {
    $class = preg_replace('/(_m|_model)?$/', '', get_class($this));
    $this->_table = strtolower($class);
  }
}

Nothing 'wrong' per se with your approach.
#7

[eluser]taggedzi[/eluser]
After a good deal of thinking. I have decided that I will leave this in its "library" form for now. This allows developers to extend "MY_Model" very easily (if they want too), but does not force them to load something or do calls a particular way. They could even chose to auto detect the table if they wanted too... but I would not include that into the general library.

I think this offers the greatest flexibility for the developer to implement it as they need on a per case basis, without forcing them to implement it in any specific way that might be counter-intuitive to what they need or want on a particular project.

I can certainly see the usefulness of your suggestion in certain circumstances... but I can also see where I would not like it in others. Because it is so easy to modify (or use to extend the Model class)... I will leave it as is unless I find a better way to do it, I trust the developers to implement it as they need, be that as a library or an extension of the CI core classes.

Thanks for the input, it is much appreciated.




Theme © iAndrew 2016 - Forum software by © MyBB