Welcome Guest, Not a member yet? Register   Sign In
Quick Question: Multilple Models in a single Controller
#1

[eluser]Unknown[/eluser]
Is it considered bad practice to use multiple models within one controller? The situation I'm referring too is where tables have a "one-to-many" relationship and the view would need data from both tables.

Should the calls be inside the model? Resulting in a single model call in the controller, but a more complex DB call in the model. Or should the controller use both models and make the call to their respective models?

I realized this could be a matter of opinion, but I don't want to stray away from best practices.
Thanks!
#2

[eluser]jedd[/eluser]
Hi JCBarry and welcome to the forums.

I think it's going to be a combination of personal taste & preference, along with a question of degree (thinking weightiness of separate models versus weightiness of controller code).

I don't know if there's a best practice per se.

I know that if you head down a proper AR path you tend to end up with a 1:1 table to model situation.

I also know that my models rarely front a single table (at the moment I have only one such situation). They'd normally front about 3 or 4, but I have one model that fronts 24 tables. (Most are simple, similar, and all are very much related.)

Having said that I'd guess your best bet, starting out, as I did, would be to try to keep model functionality separate, load the two of them if you need them both within a controller, and see how that works out.
#3

[eluser]Unknown[/eluser]
Thanks Jedd!
I went ahead with multiple models in the controller. It seemed to make the most sense initially.

Thanks again for your insight.
#4

[eluser]jdfwarrior[/eluser]
This is just personal preference, but I tend to break models up depending on the actions they perform. For instance, I have a single model that handles user related stuff like adding/setting user data, removing a user, checking user existence, etc. If its not user related, its not in that model. Results: Loading multiple models in the controller, depending on what the controller is doing.

Just how I do it..
#5

[eluser]TheFuzzy0ne[/eluser]
Same here. One of the controllers in my forum requires 7 different models - Forum, post, topic, user, front page, post tracker and email notifications.
#6

[eluser]Unknown[/eluser]
Hey,

I'm using more than 1 model in my controller, but I need to send the data to the view... So here is my Problem: How do I do this?

To be more precise, I have a Blog post, the data comes from message_table. It's like "headline", "text", "author_id", "created" and I can send this data to my view but now I want to get the name of the author from the user_table ('author_id', 'name', 'screen_name', 'avatar') into my view...
It's the same with the Comments and the Comments author...

Any help/ideas?
#7

[eluser]jdfwarrior[/eluser]
[quote author="StefanRoehle" date="1248902008"]Hey,

I'm using more than 1 model in my controller, but I need to send the data to the view... So here is my Problem: How do I do this?

To be more precise, I have a Blog post, the data comes from message_table. It's like "headline", "text", "author_id", "created" and I can send this data to my view but now I want to get the name of the author from the user_table ('author_id', 'name', 'screen_name', 'avatar') into my view...
It's the same with the Comments and the Comments author...

Any help/ideas?[/quote]

Not quite sure I understand what your asking. If you can send data to your views already, there shouldn't be a problem. Call the model functions, return the data into a variable, pool all your data together in a single array in the controller and then pass that array to the view the same way you do now.
#8

[eluser]ToddyBoy[/eluser]
I have a similar situation. I'm building a CMS for a niche market. The CMS will allow the user to add blog posts, add events to a calendar, create polls, manage staff, etc. I can get this to work as each page of the CMS will only require details from one model inside one controller sending the data to a single view.

The problem is for the actual site that the CMS will be source of data. For example, the home page requires recent blog posts, upcoming calendar events and the main content of the page. The way I have set it up is to have a model for each 'module' of the CMS - recent blog posts, upcoming calendar events, etc. From reading the user guide, that is what models are for - interaction with a database. I would then pass the results of the function in the model to a view to be displayed. BUT, then I need to somehow get each view 'module' to display in the main content view of the controller. Here is what I mean...

Model - blog_widget_model.php
Code:
<?php

class Blog_widget_model extends Model {

  function Blog_widget_model()
  {
    parent::Model();
  }
    
  function display()
  {
    $sql = "SELECT * FROM blog LIMIT 0,3";
    $blog_widget = $this->db->query($sql);
    return $blog_widget;
  }
}
?>

Controller - home.php (my default controller)
Code:
<?php

class Home extends Controller {

  function Home()
  {
    parent::Controller();
        
    $this->load->helper('url');
    $this->load->helper('form');
  }
    
  function index()
  {
    $this->load->model('blog_widget_model');
    $data['blog_model'] = $this->blog_widget_model->display();
    $data['topWidget'] = $this->load->view('widgets/blog', $data, TRUE);

    $this->load->model('calendar_widget_model');
    $data['calendar_model'] = $this->calendar_widget_model->display();
    $data['middleWidget'] = $this->load->view('widgets/calendar', $data, TRUE);

    $this->load->model('content_model');
    $data['content_model'] = $this->content_model->display();
        
    $this->load->view('header');
    $this->load->view('nav');
    $this->load->view('content', $data, TRUE);
    $this->load->view('footer');
  }
}
?>

The model and widget view for the calendar is basically the same as the blog model and widget view already shown.

View - widgets/blog.php
Code:
<h2>Blog</h2>

  &lt;?php foreach($data->result() as $row): ?&gt;
  
    <h3>&lt;?=anchor('blog/' . $row->blog_link, $row->blog_title);?&gt;</h3>
    
    <p>&lt;?=$row->blog_excerpt?&gt;</p>
  
  &lt;?php endforeach; ?&gt;

Inside the content view, I have the following code where I would like the blog widget to display.
Code:
&lt;?=$topWidget?&gt;

The reason I have called it $topWidget as on other pages of the site, the blog widget won't necessarily be required, so I can display another widget instead, but still have a generic term in the main content view, as the widget code is generated earlier in the controller for that page.

The outcome of all of this is the header and nav views load correctly but the content view is blank. I've taken the calendar and content models out to see if the blog model is the problem but I can't tell if it is or isn't.

Am I asking CodeIgniter to do something it can't? From my logical point of view, I can't see too many reasons why a structure like this would not work. It's probably just a matter of changing a variable or array name to something slightly different, I'm not sure, but it's driving me nuts! I need another set of eyes with more CodeIgniter knowledge to look at it for me.

So, am I close or a long way off with the code I have above?
#9

[eluser]ToddyBoy[/eluser]
After reading the user guide again and threads on this forum, it seems as though I need something like a MY_Controller class but I can't get my head around it.

Can anyone explain it in simple terms?
#10

[eluser]John_Betong[/eluser]
[quote author="ToddyBoy" date="1249600584"]After reading the user guide again and threads on this forum, it seems as though I need something like a MY_Controller class but I can't get my head around it.

Can anyone explain it in simple terms?[/quote]
&nbsp;
&nbsp;
I would be tempted to:
&nbsp;
1. ensure your config.php -> $config['subclass_prefix'] = 'MY_'; case matches the filename and also your class name.
2. try creating your own MY_Controller.php. Did I mention it is case sensitive.
3. save your class in your ./applications/libraries/ directory
4. if it loads automatically with no errors then extend your personal class
5. try calling and passing parameters to your personal controller.
&nbsp;
Any problems then first search this forum or start a new thread, explain what steps you have taken, include the code and ask for help.
&nbsp;
Once you have wrapped your head around extending classes you will no doubt find that it is well worth the effort because it reduces the amount of repetious code tremendously.
&nbsp;
&nbsp;
&nbsp;




Theme © iAndrew 2016 - Forum software by © MyBB