Welcome Guest, Not a member yet? Register   Sign In
Models - pain in the ass
#1

[eluser]pkorzeniewski[/eluser]
Hi,

Some time ago I wrote about models, I was a bit confused about their usefulness cause so far I simply didn't use them. Instead of models, I used libraries with smaller methods which then I used in controllers - it worked well, but I felt like missing the point of MVC. So, I decided to try using models as everyone stated I should use them for better organised applications but well.. models for me are pain in the ass and I don't know if I just miss the concept of models or what. Let's take a simple and common example - a blog module. First method you would put in model is get_entries which simplified looks smth like this:

function get_entries() {
$entries_query = $this->db->get('blog_entries');
$entries_data = array();

foreach($entries_query->result() as $entry) {
$entries_data[] = $entry;
}

return $entries_data;
}

Ok, no problem here - but what I would like to add sorting by some field? I see two options here:

1. Create additional methods:

function get_entries_by_date()
function get_entries_by_name()
function get_entries_by_author()
...

2. Add parameters to get_entries:

function get_entries($order_by = array()) {
if(isset($order_by) && is_array($order_by)) {
$this->db->order_by($order_by[0], $order_by[1]);
}
// ...
}

Still no big problem - but what if would also like to filter entries by author/category/etc AND sort them AND get entries between given date? Creating more methods isn't the option here, so I would need to add more parameters but this is also stupid cause usually I wouldn't use all of them so I would end with using get_netries like:

get_entries(null, null, null, some_parameter, null, another_parameter);

Of course I can use one parameter called "options" which would be an associative array but again - what if I would like to create method that retrieves all categories filtered by some fields AND all entries filter by some fields assigned to each topic?

What I want to point here is the lack of flexibility - I can either write specialised methods while most of them will be used only once in application, or I can write less methods with more parameters which are shitty to use and most likely I will often modify them to add new parameters. With libraries I can simply create smaller, more flexible methods and use them in controllers in many different ways. So for example I can obtain some data using Active Records just like I want, then pass the result to some small methods in libraries and finally pass it to view. No big, complex models - just small libraries with methods which I can use in many different ways to obtain same result as in models, with far more flexibility.
#2

[eluser]danmontgomery[/eluser]
Not trying to be harsh, but I don't think you're just missing the point of models, I think you're missing the point of object oriented programming.

Why do:

Code:
$this->blog->get_entries(null, null, null, some_parameter, null, another_parameter);

When you can:

Code:
$this->blog->order_by = "date DESC";
$this->blog->limit = 5;
$entries = $this->blog->get_entries();
#3

[eluser]octavianmh[/eluser]
Until this very clear example, I had forgotten this little (and by little, I mean gigantic) detail of OOP as well, thanks for the post, it made me facepalm (in a good way). Smile

Yay for better models!




Theme © iAndrew 2016 - Forum software by © MyBB