Welcome Guest, Not a member yet? Register   Sign In
Calling Model Function in the View
#1

[eluser]Gnurpreet[/eluser]
Hi,
Can I call a function from a model to directly into the view? I have loaded it in the controller, but want to call the function in the view only.

Here's the situation...
I have a showing a piece of content in the view, but I have to show the name of the user who posted it (which comes from a different table and thus a different user model ). Now, in the view, I have the ID of the user, but I want to show his username.

This in the view does not work
Code:
echo "Posted by ".$this->User_model->get_username($list->user_id);

I have loaded the view in the controller, but still.

Here is the model code
Code:
class User_model extends Model
{
    function User_model()
    {
        parent::Model();
    }
    
    function get_username($user_id)
    {
        $this->db->where("id", $user_id);
        $user_data = $this->db->get("user");
        return($user_data['username']);        
    }
}

any pointers?
#2

[eluser]Gnurpreet[/eluser]
An I think that this will be a useful thing to have because with every piece of content (of various types), I will have to name the author. So one function in a seperate user model would just do great.

What do you guys think?

Gupreet
#3

[eluser]Rick Jolly[/eluser]
Normally the controller provides all the data for the view - that is the MVC way. I would do an sql join on the 2 tables in the controller. Then the controller could pass all that related user data to the view. It is more efficient than 2 queries as well.
#4

[eluser]Gnurpreet[/eluser]
Yes, I know what you mean. I am new to frameworks, but in a normal application (not very trictly MVC), I would have this one utility function which would be very helpful throughout the app.

Helpful...
Is it possible to make application specific helpers? Maybe that is the answer Smile
#5

[eluser]Rick Jolly[/eluser]
Calling helpers from the view to format/modify existing data for presentation is fine and often necessary. However, calling a helper from the view to get data violates MVC. Also, to create a helper that just calls a model seems unnecessary when you could call the model directly from the controller.

I don't see how getting the data in the controller and sending it all to the view is any less convenient. But maybe if you posted all your code you could prove me wrong.
#6

[eluser]coolfactor[/eluser]
I see why you're using this approach.

1. You load a bunch of posts
2. As you output each post, you grab the poster's name using your User_model.

Don't be afraid to iterate over your data in the controller before passing it to the view. I know there is a fixation on performance that blinds many developers to the true speed of today's servers and the lack of harm done in pre-processing the data.

1. Load your posts in the controller.
2. Iterate over the resultset, and retrieve the User's name on each iteration, assigning each post to a new array along with the User's name.
3. Pass that new array to the view.

Of course, a MySQL join operation would eliminate that extra work.
#7

[eluser]Gnurpreet[/eluser]
Ok chief, will do as you say. I am kinda getting that feeling that I will not understand this now, but will thanks you for this later Smile
g




Theme © iAndrew 2016 - Forum software by © MyBB