CodeIgniter Forums
New CI User - need assistance with Model issue please. - 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: New CI User - need assistance with Model issue please. (/showthread.php?tid=40580)

Pages: 1 2


New CI User - need assistance with Model issue please. - El Forum - 04-12-2011

[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!


New CI User - need assistance with Model issue please. - El Forum - 04-13-2011

[eluser]InsiteFX[/eluser]
Did you try to autolaod the database in application/config/autoload.php ?
You can also autoload your Model.

InsiteFX


New CI User - need assistance with Model issue please. - El Forum - 04-13-2011

[eluser]Mirge[/eluser]
[quote author="InsiteFX" date="1302715972"]Did you try to autolaod the database in application/config/autoload.php ?
You can also autoload your Model.

InsiteFX[/quote]

Sorry, forgot to mention that. Yes, I autoloaded DB. Did not autoload model... following that jeffrey way day 1 tutorial, he didn't.... so I didn't. I commented out $this->CI->load->db(...) and it still works of course.

I just don't understand why I have to use get_instance() in the model? Am I doing something wrong?


New CI User - need assistance with Model issue please. - El Forum - 04-13-2011

[eluser]InsiteFX[/eluser]
You should not have to use get_instance in the model, I just wrote a model last night and did not have to use get_instance. If your creating a library then some times you need get_instance.

Model:
Code:
&lt;?php

class Site_model extends CI_Model
{
    function getAll()
    {
        //$this->CI = get_instance();
        
        // $this->CI->load->database(); you do not need this here your loading the database in your controller
        $q = $this->db->get('test');
        
        if($q->num_rows() > 0)
        {
            // should be $q->result_array()
            foreach($q->result_array() as $row)
            {
                $data[] = $row;
            }
            
            return $data;
        }
    }
}

InsiteFX


New CI User - need assistance with Model issue please. - El Forum - 04-13-2011

[eluser]Mirge[/eluser]
[quote author="InsiteFX" date="1302725864"]You should not have to use get_instance in the model, I just wrote a model last night and did not have to use get_instance. If your creating a library then some times you need get_instance.

Model:
Code:
&lt;?php

class Site_model extends CI_Model
{
    function getAll()
    {
        //$this->CI = get_instance();
        
        // $this->CI->load->database(); you do not need this here your loading the database in your controller
        $q = $this->db->get('test');
        
        if($q->num_rows() > 0)
        {
            // should be $q->result_array()
            foreach($q->result_array() as $row)
            {
                $data[] = $row;
            }
            
            return $data;
        }
    }
}

InsiteFX[/quote]

Thanks, Insite... I greatly appreciate your help. Are you using CodeIgniter Reactor 2.0.2? Just making sure. I can't figure out why it won't work for me. using XAMPP.. maybe I should try my paid hosting account instead to test.


New CI User - need assistance with Model issue please. - El Forum - 04-13-2011

[eluser]InsiteFX[/eluser]
Yes I am using CodeIgniter Reactor 2.0.2 on Windows 7 Pro x64-bit and XAMPP 1.7.3

Give me a little time, I'll copy what you have here and see if I can find out whats wrong. I already have a test database so I can use that...

InsiteFX


New CI User - need assistance with Model issue please. - El Forum - 04-13-2011

[eluser]Atas[/eluser]
put this on top of the index.php

ini_set('display_errors', '1');


New CI User - need assistance with Model issue please. - El Forum - 04-13-2011

[eluser]Mirge[/eluser]
[quote author="InsiteFX" date="1302726585"]Yes I am using CodeIgniter Reactor 2.0.2 on Windows 7 Pro x64-bit and XAMPP 1.7.3

Give me a little time, I'll copy what you have here and see if I can find out whats wrong. I already have a test database so I can use that...

InsiteFX[/quote]

Exact same setup as me, but using XAMPP 1.7.4... though I imagine that has nothing to do w/ my issue.

Thanks again for the help! Just started playing w/ codeigniter, and ran into this issue, discouraging... but the benefits of using CI far out-weigh this hurdle I can already tell.

My 'test' table simply contains:

id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY NOT NULL,
title VARCHAR(255) NOT NULL


New CI User - need assistance with Model issue please. - El Forum - 04-13-2011

[eluser]Mirge[/eluser]
[quote author="Atas" date="1302726710"]put this on top of the index.php

ini_set('display_errors', '1');[/quote]

ini_set('display_errors', 'on');
ini_set('display_startup_errors', 'on');

error_reporting(E_ALL);

No new information... just same error.


New CI User - need assistance with Model issue please. - El Forum - 04-13-2011

[eluser]InsiteFX[/eluser]
@Mirge,

Ok, I just ran your code and it works here!

Controller:
Code:
&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Site extends CI_Controller {

    // Class Variables.
    // --------------------------------------------------------------------


    // --------------------------------------------------------------------

    /**
     * __construct()
     *
     * PHP 5+    Constructor
     *
     * You do not really need this unless you setting up stuff!
     *
     * @access    public
     * @return    void
     */
    public function __construct()
    {
        parent::__construct();
    }

    // --------------------------------------------------------------------

    /**
     * index()
     *
     * Default Controller index method.
     *
     * @access    public
     * @return    void
     */
    public function index()
    {
        $this->load->database();
        $this->load->model('site_model');

        $data['records'] = $this->site_model->get_all();

        $this->load->view('home', $data);
    }

}

// ------------------------------------------------------------------------
/* End of file name.php */
/* Location: ./application/controllers/site.php */

Model:
Code:
&lt;?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Site_model extends CI_Model {

    // --------------------------------------------------------------------

    /**
     * Class Variables.
     *
     * protected, private, public and static variables.
     * --------------------------------------------------------------------
     */

    private $CI;


    // --------------------------------------------------------------------

    /**
     * __construct()
     *
     * PHP 5+    Constructor.
     *
     * You do not really need this unless you setting up stuff!
     *
     * @access    public
     * @return    void
     */
    public function __construct()
    {
        parent::__construct();

        // Get the CI Super Global Object to a local variable for use later.
        $this->CI =& get_instance();

    }

    public function get_all()
    {
        $query = $this->db->get('posts');    // change to your test!

        if ($query->num_rows() > 0)
        {
            foreach ($query->result_array() as $row)
            {
                $data[] = $row;
            }

            return $data;
        }
    }
}

// ------------------------------------------------------------------------
/* End of file Site_model.php */
/* Location: ./application/models/site_model.php */

View:
Code:
<!DOCTYPE html>
&lt;html lang="en"&gt;
&lt;head&gt;
    &lt;meta charset="utf-8"&gt;
    &lt;title&gt;Welcome to CodeIgniter&lt;/title&gt;

&lt;style type="text/css"&gt;

body {
background-color: #fff;
margin: 40px;
font-family: Lucida Grande, Verdana, Sans-serif;
font-size: 14px;
color: #4F5155;
}

a {
color: #003399;
background-color: transparent;
font-weight: normal;
}

h1 {
color: #444;
background-color: transparent;
border-bottom: 1px solid #D0D0D0;
font-size: 16px;
font-weight: bold;
margin: 24px 0 2px 0;
padding: 5px 0 6px 0;
}

code {
font-family: Monaco, Verdana, Sans-serif;
font-size: 12px;
background-color: #f9f9f9;
border: 1px solid #D0D0D0;
color: #002166;
display: block;
margin: 14px 0 14px 0;
padding: 12px 10px 12px 10px;
}

&lt;/style&gt;
&lt;/head&gt;
&lt;body&gt;

<h1>Welcome to CodeIgniter!</h1>

<p>The page you are looking at is being generated dynamically by CodeIgniter.</p>

<p>If you would like to edit this page you'll find it located at:</p>
<code>application/views/welcome_message.php</code>

<p>The corresponding controller for this page is found at:</p>
<code>application/controllers/welcome.php</code>

<p>If you are exploring CodeIgniter for the very first time, you should start by reading the <a href="user_guide/">User Guide</a>.</p>

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

<p>&lt;?php echo 'CodeIgniter Reactor ver: '.CI_VERSION;?&gt;</p>

<p>Page rendered in {elapsed_time} seconds</p>
<p>Memory Usage: {memory_usage}</p>

&lt;/body&gt;
&lt;/html&gt;

InsiteFX