Welcome Guest, Not a member yet? Register   Sign In
What is the correct way to code in CodeIgniter
#1

[eluser]WebMada[/eluser]
Hi!

Is there a good article on that you know?

I talk about to put all code in the right place: in the views no complex algorithms just echos and loop of echos, in the controllers the right code of controller and in the models the right code to put in models. To respect the spirit of M V C!

Till now, for me, a model corresponds to a CRUD of a database table. But when I look at CI-based CMS and other proprietary CI-based applications, their views/controllers/models subdirectories are classified in several directories, they create models for everything and controllers are just uses of models.
#2

[eluser]WanWizard[/eluser]
When it comes to the controller/model discussion, there are basically two camps:

- fat controller / lean model

In this scenario the model doesn't do much more then some basis I/O (get, insert. update), all other logic, like query building, data selection and conversion, validation, is done in the controller

- lean controller / fat model

The opposite of the above. Controllers don't do much more then controlling the flow from request to response, fetching model info and passing it on to a view. The model contains functional logic, such as get_active_clients(), which not only builds the query and the selection, but also returns the data in a unified format directly suitable for the view. In case of inserts or updates, the model also does the validation of the input, to ensure all data in the database is correct.

I am a supporter/user of the last scenario. It is more DRY in that no matter where the data comes from or who requests it, there is only one piece of code (the method in the model) responsible for it. It also hides the datamodel from your controllers. as long as your model methods return what they should, it doesn't matter if the data comes from one or ten tables, or perhaps a REST service. Your controller doesn't care, and doesn't need to be changed when your data model changes.

#3

[eluser]PhilTem[/eluser]
But I'd like to mention that you still have the opportunity to use libraries, which means you got one more camp

fat controller / lean library / lean model

lean controller / fat library / lean model

lean controller / lean library / fat model

I'd locate all my code somewhere between camp two and three even though my models themselves are pretty pretty lean since MY_Model is very huge. Compared to this all models are kinda lean, and I end up having more or less fat controllers or fat libraries (depending on the business logic for each lib, e.g. my user authentication and access-control-list library is very fat, other libraries are very thin.

All in all I cannot really say what "the correct way to coding CI" is, it's a more subjective answer and you gotta find a way for yourself to coding comfortably Wink
#4

[eluser]WanWizard[/eluser]
Libraries technically aren't part of MVC, they are CI's way of implementing helper classes.

I don't think data processing should take place in a helper class, it's an integral part of the model. There is no advantage to moving code from the Model (which is a singleton in CI) to a Library (which is also a singleton). The code is exactly the same, you only add an extra layer, and use $this->library->method instead of $this->model->method.
#5

[eluser]WebMada[/eluser]
I am back on this topic.

My real question is: how to better represent database tables when coding in CI?
#6

[eluser]Aken[/eluser]
Kind of a generic question. How would you prefer to represent database tables? You can create models that use the built-in query builder (active record) to perform your database actions. You can look into a base model to built your models on top of to make life a little easier, or an ORM for a more direct relationship.
#7

[eluser]WebMada[/eluser]
I recently discover "Bonfire" http://cibonfire.com/

I gonna see it and then inspire it.




Theme © iAndrew 2016 - Forum software by © MyBB