Welcome Guest, Not a member yet? Register   Sign In
Models - useful?
#1

[eluser]pkorzeniewski[/eluser]
Hi!

I use CI for some time now but I'm still a bit confused about models. So far I wasn't using models at all, if I had to obtain some data from DB I simply used AR directly in controllers, in more complex situations - I wrote libraries. This worked well and I didn't have any problems (even in big applications/sites), however I feel like I'm approaching MVC the wrong way cause everyone uses models but for me it just brings more complexity to application than it's worth. Or am I missing something? Could you explain me why models are useful? Why create additional methods for simple tasks that aren't used often anyway when I can simply use AR and adjust everything as I want rather than write many sophisticated methods?

Thanks,
Peter
#2

[eluser]Twisted1919[/eluser]
Well , models are useful when you do repetitive tasks in distinct places .
Personally , i just don't like to see any sql code into my controllers ... without that , i have a cleaner code , i guess i've just like this way , so it's all about organization.

For instance , having an app that display the username on every page , would require you to write sql queries for each of your controllers right ?
Why do that ? when you can create a model , place a function that retrieve the username , then from your controllers just call that function .
Later on , you will decide you want to show he's last login , if you hadn't used models , you would need to change sql code on every controller , but with models , changing just a method is enough .

Hope you understand my point of view . The example with that username, is of course a silly one , but will make things clear .
#3

[eluser]kgill[/eluser]
From the sounds of it, you've sort of used a library where the rest of us would use a model. That aside the benefit of a model has less to do with your code than it has to do with maintaining it. Imagine you have to change something about the data, if all the code that deals with that object is in one place you just edit that file. Because you've scattered that logic through all your controllers instead of fixing it in one spot you've got to go through all your code and make changes in each file (hoping that you've gotten all the references). That is a maintenance nightmare on a large project.

The other benefit is code re-use, suppose you have a new project that needs to interface with data in the same manner of a past project, you can take that model and drop it into the new project, all the functions that deal with that object are already written and in one spot ready to be used, no model means you have to duplicate previous efforts and that's just wasting time.

That's the simplified version of things but there is a reason why MVC starts with an M, not using it kind of defeats the whole purpose.
#4

[eluser]Colin Williams[/eluser]
Modeling is really a concept a child should understand. Models know what data looks like, how it relates, and how it can be manipulated and stored. If someone asked you, "Can you model project management for me?" your model would be something like:

There are multiple projects. Projects have unique tasks, assigned to different users. Tasks have comments created by users. Tasks may be grouped by milestones. And so on..

Now, turn that into code. That's what your model files will do. (Note: Don't think of each model file as the model layer. It's most likely that you will use multiple model files and classes to make up the model layer. Same is true for controllers and views).

The model really is the machine, views are the interface to that machine, and the controller handles the communication between them. Libraries and helpers are there to help these three components finish their tasks. Keep it simple.
#5

[eluser]pkorzeniewski[/eluser]
Thank you very much for your replies, now when you explained me how application structure based on MVC pattern should be organised, I realised I simply used libraries where I should use models (what kgill has pointed out) and while it worked, it's just easier and makes more sense to use models.

Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB