CodeIgniter Forums
Calling Model From View File? - 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: Calling Model From View File? (/showthread.php?tid=18180)

Pages: 1 2


Calling Model From View File? - El Forum - 04-28-2009

[eluser]atlanta[/eluser]
Hi everyone,

I kinda new to the framework, but I am really liking it but i ran into a problem i need a little help on let me explain. Ok i have a page where users can post comments and when they post it saves there user_id to the comment entry in the DB.
The problem im having is when i retrieve the comments for a certain topic i get the id but i don't know how to call my get_user_info(id) model function from the view to retrieve the user name. could someone guide me to what structure i should use to achieve this.

Thanks
-Will


Calling Model From View File? - El Forum - 04-28-2009

[eluser]Dam1an[/eluser]
You should never call models from the view
You should call the model in your controller which could join the comments and users table, and pass the result of that to the view


Calling Model From View File? - El Forum - 04-28-2009

[eluser]TheFuzzy0ne[/eluser]
It's advisable not to call any model methods from within your view. You should pass the data from the model into the view from your controller.

If you can post some code, I should be able to offer a bit more help.


Calling Model From View File? - El Forum - 04-28-2009

[eluser]Colin Williams[/eluser]
It's not terrible to use the model from view files, you just don't want to "alter the state of the model." This means don't run queries, fetch new data, etc.


Calling Model From View File? - El Forum - 04-28-2009

[eluser]atlanta[/eluser]
story.php (Controller)
Code:
class Story extends Controller {

    var $categories;
    function Story()
    {
        parent::Controller();
        $this->load->model('story/stories_model');
    }

function id ()
    {
        $query = $this->stories_model->get_story_info($this->uri->segment(3));
        if ($query->num_rows() > 0)
        {
           $data['info'] = $query->row_array();
        }
        else
        {
            redirect('story/');
        }
        $data['title'] = $data['info']['title'];
    $data['extraHeadContent'] = '<link href="'.site_url("css/rating_style.css").'" rel="stylesheet" type="text/css" media="all">';
    $data['extraHeadContent'] .= '[removed][removed]';
        
         $comments = $this->stories_model->get_story_comments($this->uri->segment(3));
         $data['comments'] = $comments->row_array();
        $this->load->view('story/single', $data);
    }
}

stories_model.php (Model)
Code:
class Stories_model extends Model {

    function Stories_model()
    {
        parent::Model();
        $this->_prefix = $this->config->item('dbprefix');
    }
    
    function get_story_comments($id)
    {
        $this->db->where('story_id', $id);
        return $this->db->get('comments');
    }
        
}

single.php (View)
Code:
<?
$this->load->view("header");
include(".xx..xx...rating_functions.php");
$comment = array(
                        'name'    => 'comment',
                        'id'    => 'comment',
                        'cols'    => 25,
                        'rows'    => 10,
                        'value' => set_value('comment')
                    );
?>


<h1>&lt;?=$info['title']?&gt;</h1>

<br />

&lt;?=$info['story']?&gt;
<br />

&lt;? echo pullRating($info['id'],true,false,true); ?&gt;

<br />
Post Comments:
<br />
&lt;?
if ( ! $this->dx_auth->is_logged_in())
{
echo "Must be logged in to post comments";
}
else
{
?&gt;
    &lt;form name="comments" action="&lt;?=site_url('story/postcomment');?&gt;" method="POST"&gt;
    &lt;input type="hidden" name="story_id" value="&lt;?=$info['id']?&gt;" /&gt;
    &lt;?
    echo form_textarea($comment);
    ?&gt;
    <br />
    &lt;?echo form_submit('submit', 'Submit Comment!');?&gt;
    &lt;/form&gt;
&lt;?
}
?&gt;

&lt;?
$this->load->model('dx_auth/users');
&lt;?=$this->dx_auth->get_user_by_id($comments['user_id']?&gt;
&lt;?=$comments['comment']?&gt;

&lt;?
$this->load->view("footer");
?&gt;



Calling Model From View File? - El Forum - 04-28-2009

[eluser]atlanta[/eluser]
Hopefully that helps.


Calling Model From View File? - El Forum - 04-28-2009

[eluser]Zeeshan Rasool[/eluser]
you can call model function from view , and it works but its not good approach . some times we need some variant record in a foreach loop so we have to use this technique in view.
I also use it some times.


Calling Model From View File? - El Forum - 04-28-2009

[eluser]atlanta[/eluser]
Ok so ill stay away from calling the model in the view... but how should i go about getting the username (Linking the data).


Calling Model From View File? - El Forum - 04-28-2009

[eluser]Zeeshan Rasool[/eluser]
if you need , then use it.


Calling Model From View File? - El Forum - 04-28-2009

[eluser]atlanta[/eluser]
Well the thing is i want to do it the right way and when i try to use it in the view i get error messages.. but then when i use it in the controller it calls the function fine. but im not sure about how to make it work in the controller and pass it to the view.