CodeIgniter Forums
Two trivial questions (about models and controllers) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Two trivial questions (about models and controllers) (/showthread.php?tid=1895)



Two trivial questions (about models and controllers) - El Forum - 07-03-2007

[eluser]Marcus Cavalcanti[/eluser]
CI People,

I have two questions and i think they can help me.

First question:

I have a site with frontend (users view) and backend (admin view), probably i need to use the models in this two cases (backend and frontend). My question is: How to use the same models in this two cases?

Second question:

In my fronted, my site have's a header, menu, content and footer, it is a classic model site, and the unique container has change is the content. Mu question: The best way to make this is create a "My_Controller" that extends the CI Controller, and the pages Controlllers (blog, forum, contact) extends "My_Controller"?

Thanks!


Two trivial questions (about models and controllers) - El Forum - 07-03-2007

[eluser]Marcus Cavalcanti[/eluser]
anyone try help me?


Two trivial questions (about models and controllers) - El Forum - 07-03-2007

[eluser]Christopher Blankenship[/eluser]
Okay, I haven't tried it in CI yet (so forgive me if I am wrong), but I have loaded additional models for use in controllers and also loaded models by other names into a controller within another framework which has quite a few similarities. Pulling only the information from the model that was needed into that particular controller.

//UPDATE
Here are some links.
http://ellislab.com/forums/viewthread/47867/
http://ellislab.com/forums/viewthread/46837/
http://ellislab.com/forums/viewthread/54058/


Two trivial questions (about models and controllers) - El Forum - 07-04-2007

[eluser]Marcus Cavalcanti[/eluser]
humm .. I do not agree, because in my conception models they represent entities from database, example:

I have a user entity and i have model for this, in my frontend i can use this model to get informations about user and in my backend i using the same model to add/edit/remove or get informations about user, so i that to use the same model in two "applications".

I am right or wrong?


Two trivial questions (about models and controllers) - El Forum - 07-04-2007

[eluser]Christopher Blankenship[/eluser]
Well it sounds like you are setting up 1 to 1 model. You can setup additional things within the same model just as you can with controllers.

extend the model as follows and this is only an example.
Code:
class model_name extends Model {
  function model_name()
  {
    parent::Model();
  }

    function index()
    {
      // code here
    }

    function return_users()
    {
      // code here
    }

    function update_user($id)
    {
      // code here
    }
}
Then from your controller call the correct function within the model.
$this->load->model('model_name/return_users');


Two trivial questions (about models and controllers) - El Forum - 07-04-2007

[eluser]Marcus Cavalcanti[/eluser]
Ok, i understand. But my doubt is in use the same model in two different applications, in this case backend and frontend .. It is possible?

And second uestion .. anybody help me?


Two trivial questions (about models and controllers) - El Forum - 07-04-2007

[eluser]Christopher Blankenship[/eluser]
It would not be using the same model in two different applications the whole system would be one application; the only difference would be the information being delivered for each section members and admin. Therefore you will need to setup your model call in a way to deliver what you need for that call. I as well as many others will be of assistance as we can ... I am however fairly new to CI and this forum but not to frameworks and each do have some differences.


Two trivial questions (about models and controllers) - El Forum - 07-04-2007

[eluser]the real rlee[/eluser]
[quote author="Marcus Cavalcanti" date="1183505020"]CI People,

I have two questions and i think they can help me.

First question:

I have a site with frontend (users view) and backend (admin view), probably i need to use the models in this two cases (backend and frontend). My question is: How to use the same models in this two cases?

Second question:

In my fronted, my site have's a header, menu, content and footer, it is a classic model site, and the unique container has change is the content. Mu question: The best way to make this is create a "My_Controller" that extends the CI Controller, and the pages Controlllers (blog, forum, contact) extends "My_Controller"?

Thanks![/quote]

As I'm sure you are aware Models are used for data retrieval and data manipulation - such as that from a db. I often create Models for each database entity. So when a Controller works with that entity i load the related Model irrespective of whether that Controller deals with "public" or "admin" functionality.

Having a base Controller like this often makes sense when your Controllers share common function or information. In the past I've used a "base" Controller to load Views common to all Controllers and set common variables i.e. constants.

Goodluck!


Two trivial questions (about models and controllers) - El Forum - 07-05-2007

[eluser]NemetraL[/eluser]
You can load any model from any controller.

CI is very flexible and you can organize your web application as you want. Some create one model per controller, others one model per database entity... I think the point is to avoid repeating the same function in several models because in case you want to change it you'd have to change it two times (that's DRY).

More generally, when I realize one of my models may be quite widely used throughout the application or even in different applications, I tend to make a library out of it. Ah yes.. I also use my models in a more global way than just retrieving data from the db, hence my libraries built out of models. If you prefer not to code this way, just focus on my first paragraph ;-).

Happy coding :-)