Welcome Guest, Not a member yet? Register   Sign In
New CI User - need assistance with Model issue please.
#1

[eluser]Mirge[/eluser]
Controller (site.php):
Code:
<?php

class Site extends CI_Controller
{
    function index()
    {
        $this->load->database();
        $this->load->model('site_model');
        $data['records'] = $this->site_model->getAll();
        
        $this->load->view('home', $data);
    }
}

Model (site_model.php):
Code:
<?php

class Site_model extends CI_Model
{
    function getAll()
    {
        $this->CI = get_instance();
        
        $this->CI->load->database();
        $q = $this->CI->db->get('test');
        
        if($q->num_rows() > 0)
        {
            foreach($q->result() as $row)
            {
                $data[] = $row;
            }
            
            return $data;
        }
    }
}

View (home.php):
Code:
<p>My view has been loaded</p>
<pre>&lt;?php print_r($records); ?&gt;</pre>

---

This works, but it took a TON of google'ing to figure out this fix... and I don't understand why it's necessary? Please help me understand what is actually happening here.

If you look at the model code, you'll notice: $this->CI = get_instance();

Without this (and obviously w/out referencing it in the code following it), it breaks with a Fatal error... Fatal error: Call to a member function get() on a non-object

Any reason why exactly? I was following the nettuts videos by Jeffrey and ran into this at the end. Using CodeIgniter 2.0.2, PHP 5.3... thanks for the help!


Messages In This Thread
New CI User - need assistance with Model issue please. - by El Forum - 04-12-2011, 10:44 PM
New CI User - need assistance with Model issue please. - by El Forum - 04-13-2011, 06:32 AM
New CI User - need assistance with Model issue please. - by El Forum - 04-13-2011, 08:56 AM
New CI User - need assistance with Model issue please. - by El Forum - 04-13-2011, 09:17 AM
New CI User - need assistance with Model issue please. - by El Forum - 04-13-2011, 09:23 AM
New CI User - need assistance with Model issue please. - by El Forum - 04-13-2011, 09:29 AM
New CI User - need assistance with Model issue please. - by El Forum - 04-13-2011, 09:31 AM
New CI User - need assistance with Model issue please. - by El Forum - 04-13-2011, 09:32 AM
New CI User - need assistance with Model issue please. - by El Forum - 04-13-2011, 09:34 AM
New CI User - need assistance with Model issue please. - by El Forum - 04-13-2011, 09:56 AM
New CI User - need assistance with Model issue please. - by El Forum - 04-13-2011, 10:33 AM
New CI User - need assistance with Model issue please. - by El Forum - 04-13-2011, 10:36 AM
New CI User - need assistance with Model issue please. - by El Forum - 04-13-2011, 10:40 AM
New CI User - need assistance with Model issue please. - by El Forum - 04-13-2011, 11:39 AM



Theme © iAndrew 2016 - Forum software by © MyBB