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

[eluser]taggedzi[/eluser]
That's right I built a CRUD (Create, Retrieve, Update, and Delete) Model for Codeigniter. I have spent the last week re-writing it, and testing it to post it. I posted the files on my website along with a basic function list. I might add more to the article if there are questions or comments.

One of the powerful things about this model is you can simply pass an ARRAY of keyed data to the function and it will break the arrays up and use them intelligently. For example how many times have you written a database query and realized you needed to add a new condition to it, or organize it differently? To do so with this model, all you do is add a new element to an array and you are back in business. The array can even indicate other operators other than = such as >, <, !=, >= , <=.

So if I had a database and I wanted to query for results I would do this:

Code:
$criteria = array(
               'id >=' => 1,
               'title' => $title_string,
                'published' => 1);
$sort = array(
          'title' => 'asc',
          'date' => 'desc');
$query = $this->crud->retrieve($criteria, 5, 0, $sort);

// OR the arrays can be specified INLINE so you do not have to use the extra variables.

$query =$this->crud->retrieve(array('id >=' => 1, 'title' => $title_string, 'published' => 1), 5, 0, array('title' => 'asc', 'date' => 'desc'));

// Both of these statements are identical...
// And you can now handle the query the same way as Codeigniters Active Records

This queries the database for anything that has an ID greater than or equal to 1, with a title matching the $title string, that has a "published" value of TRUE (1). It grabs the first 5 entries and sorts them alphabetically by title, and then by date. The first array determines the criteria, and you can easily change your criteria by modifying the array. The second element is your limit (how many to grab), and the third element is the offset. The last array is the SORT array, it goes in order of keys, and can be designated 'asc', 'desc', or 'random'. And you can add as many conditions to the array as you like to make remarkably complicated requests quite easily.

The files were a bit long to post in the forum so I will link to them from here.

The article explaining how to use it, and basic information about the script is here:
http://taggedzi.com/articles/display/cru...odeigniter

If you just want to download the code directly the link is here:
http://taggedzi.com/download/file/3
It is a 13kb zip file. It contains 4 files.
Code:
/CRUD/system/application/controllers/example.php   -> An example controller showing how to use the CRUD Model
/CRUD/system/application/models/crud.php           -> The actual library
/CRUD/system/application/views/index.php           -> An example view used by the example controller
/CRUD/readme.txt                                   -> Basic documentation about the project.


I included an example controller and view (not required to use the model), so that you could see how it was used. I would be glad for input. If there are any problems let me know.

The reason this was created was, I really like the Codeigniter Active Record libraries, but found I wanted a bit more flexibility, and wanted it implemented with a CRUD interface.
#2

[eluser]taggedzi[/eluser]
Sorry to post, on my own post, but I just discovered that because this is built on the Codeigniter's active records class, you can also select specific fields for your query. (I did not build it with this in mind but just noticed it does work.)

Simply call the $this->db->select(); the way you normally would, before you call the crud function you are looking to use.

Code:
$this->db->select('field_name');
$this->crud->retrieve(array('id !=' = 0));

This would select the 'field_name' from all elements that have an ID that is not equal to 0. (basially all elements...)
#3

[eluser]Unknown[/eluser]
how can i use this with a model?
#4

[eluser]cahva[/eluser]
Heres my 2 cents.. I'm sorry but I would'nt use your crud model. Heres why:
- I dont want to have general model named crud, I want them to be related to table(s) I want to modify, select and insert
- if I need more complex queries(for example joins), I would still need to create a new model(or do $this->db stuff in controller) and could'nt use your crud model for that

I've been using this base model(original by Jamie Rumbelow):
http://bitbucket.org/philsturgeon/codeig...ase-model/

With that base model, your example would have been done something like this(Im assuming table named pages:

models/pages_model.php
Code:
class Pages_model extends MY_Model {
    
}

In controller
Code:
$this->load->model('pages_model','pages');

$title_string = 'Foo';

$criteria = array(
   'id >=' => 1,
   'title' => $title_string,
   'published' => 1
);

$p = $this->pages
        ->order_by('title','asc')
        ->order_by('date','desc')
        ->limit(5)
        ->get_many_by($criteria);

I think IMHO this is just more intuitive plus if for example I have to have joins, I just add it as a method to pages model or if I have to do something else. With your crud model I cant do that as its not related to any table.

I hope I didnt let you down with my critique Smile I see that your crud model can be use for someone, but just not for me Smile
#5

[eluser]taggedzi[/eluser]
[quote author="cahva" date="1289533952"]Heres my 2 cents.. I'm sorry but I would'nt use your crud model. Heres why:
- I dont want to have general model named crud, I want them to be related to table(s) I want to modify, select and insert
[/quote]

You could always re-name it...? But aside from the "rename" comment, if you like your models to be specific to your controllers or tables... then this would not work for you. This is the exact opposite approach, I was trying to get away from having to code the same thing in 20 different models... I suppose you could just as easily copy/paste the functions (or Save As...) into a new model each time, but this would defeat the purpose of the model.

[quote author="cahva" date="1289533952"]
- if I need more complex queries(for example joins), I would still need to create a new model(or do $this->db stuff in controller) and could'nt use your crud model for that
[/quote]

Actually you can, and I do. You can also load multiple models in the same controller, (I tested this last week...) My crud model uses the $this-db functions built into Codeigniter. So you can add/extend your own function calls quite easily. Or you can use a "second" model. (Each has its advantages/disadvantages) For me it depends on the site I'm building as too which I chose. If it is a large and complicated site, I use a second model, and only load it when I'm performing more complex tasks. If it is a smaller project, I simply add the functions to my crud. (This is particularly handy when loading data from what would normally be a separate model.)


[quote author="cahva" date="1289533952"]
I think IMHO this is just more intuitive plus if for example I have to have joins, I just add it as a method to pages model or if I have to do something else. With your crud model I cant do that as its not related to any table.
[/quote]

I find this fact that it is not "directly" associated with a specific table quite helpful. It allows me to use the same functions to perform tasks on multiple databases in a single function, while still maintaining a small code base. And don't forget this is using all of CI's functions (I have not added any new database features, I just changed the way it gets accessed.) So you can easily modify the code to perform your tasks and even the results that come back.

The reason this "model" was created is because I find I do the same tasks Over and Over and Over again for almost every site. Add/Edit/Delete (or as some prefer to call it... crud) of basic table data. I was not looking to build a database model that could perform everything, the code base for that would be huge... however I did want something that could to the basic work for me. For the more advanced stuff I (personally) prefer to have fine grain controls, and I feel that codeigniter has those controls done right. But I kept using some of those controls in the same way over and over again for the simple stuff, so I finally built a model to put it all together for me..

[quote author="cahva" date="1289533952"]
I hope I didnt let you down with my critique Smile I see that your crud model can be use for someone, but just not for me Smile[/quote]

Well any developer is sad to think that something they wrote didn't get an amazing review. However, I understand different strokes for different folks. I was not trying to make an all encompassing database controller, I was just trying to create a code base that does what I do a hundred times a day, and get it done faster. For many simple sites, I don't have to do ANY database coding anymore... And for more complex sites, I want the fine tune control having the ability to add my own functions in by either adding a second model or adding it to the crud.

The funny part... I have stopped using this model for all but my most simple sites, choosing a more "model-centric" approach. (I still use it... but only when appropriate...)

This crud model has a lot of potential, but requires a lot of the "grunt" work to be done in the controller. (A lot of programmers frown on this for some reason.) For basic "simple" sites is easy (and has easily saved me hundreds of hours...), but for more complex sites (what I have been working on recently), it is actually cumbersome. So, to answer your question: It isn't that big of a let down, as I have moved on my self. I like to think my code is still useful for someone out there, just sad that it doesn't seem to be. But such is the life of a coder eh?

It is amazing how the "philosophy" of the person coding affects the final code. The results between 2 different programmers can be identical, but the code could be worlds apart (heck even comparing code from the same programmer over a span of time... ) Well that is my 2 cents. Hope it was a good read.
#6

[eluser]taggedzi[/eluser]
[quote author="Deathwalk" date="1289518889"]how can i use this with a model?[/quote]

Well ideally you shouldn't. It is meant to be a model. Theoretically you could copy/paste the code into an existing model and use the functions. directly.

Although if you want to load a model in a model, I found a couple good posts on how to do that last night just searching google. Sorry I don't have the links anymore. But it is possible, although many programmers seem to loath the idea...
#7

[eluser]Corbee[/eluser]
cool. Thanks




Theme © iAndrew 2016 - Forum software by © MyBB