[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><?php print_r($records); ?></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!