CodeIgniter Forums
$this->load->model('Blog') - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: $this->load->model('Blog') (/showthread.php?tid=27242)

Pages: 1 2


$this->load->model('Blog') - El Forum - 02-04-2010

[eluser]ignitian[/eluser]
MODEL FILE : \application\models\blog.php
Code:
class Blog extends Model
{
    public $title   = '';
    public $content = '';
    public $date    = '';

    function __construct()
    {
        parent::Model();
    }
    
    public function get_last_ten_entries()
    {
        $query = $this->db->get('entries', 10);
        return $query->result();
    }

    public function insert_entry()
    {
        $this->title   = $this->input->post('title');
        $this->content = $this->input->post('content');
        $this->date    = time();

        $this->db->insert('entries', $this);
    }

    public function update_entry()
    {
        $this->title   = $this->input->post('title');
        $this->content = $this->input->post('content');
        $this->date    = time();

        $this->db->update('entries', $this, array('id' => $this->input->post('id')));
    }
}

CONTROLLER FILE : \application\controllers\welcome.php
Code:
class Welcome extends Controller
{
    function __construct()
    {
        parent::Controller();
    }

    function index()
    {
        $this->template->set('template1');
        $data = array('title'=>'Welcome','h1'=>'Intro');
        $this->template->show($data);
    }

    function get_news()
    {
        $this->template->set('template2');
        $data = array('title'=>'Welcome','content'=>$this->get_last_news());
        $this->template->show($data);
    }

    function get_last_news()
    {
        $this->load->model('Blog'); // THIS IS NOT WORKING
        $data['query'] = $this->Blog->get_last_ten_entries();
        return $data['query'];
    }
}

ERROR

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Welcome::$Blog

Filename: controllers/welcome.php

Line Number: 29


Fatal error: Call to a member function get_last_ten_entries() on a non-object in C:\wamp\www\site1\application\controllers\welcome.php on line 29

A simple call that is not working. Database is autoload. Any tips ?


$this->load->model('Blog') - El Forum - 02-04-2010

[eluser]Michael Wales[/eluser]
CodeIgniter translates the class names to lowercase - it's actually the line below the one you identified that is erroring:

Code:
$data['query'] = $this->blog->get_last_ten_entries();



$this->load->model('Blog') - El Forum - 02-04-2010

[eluser]ignitian[/eluser]
I still have the same error with the lowercase.
Code:
Message: Undefined property: Welcome::$blog



$this->load->model('Blog') - El Forum - 02-04-2010

[eluser]danmontgomery[/eluser]
What OS?

[edit]

nm, files named correctly, case sensitivity isn't an issue

I would throw some code in the model file and in the constructor to determine if the file is being included/class is being instantiated, and go from there


$this->load->model('Blog') - El Forum - 02-04-2010

[eluser]Michael Wales[/eluser]
Is Blog listed within this dumped out variable? See code:

Code:
function get_last_news()
    {
        $this->load->model('Blog'); // THIS IS NOT WORKING

        echo '<pre>';
        print_r($this->load->_ci_models);
        die('</pre>');


        $data['query'] = $this->Blog->get_last_ten_entries();
        return $data['query'];
    }



$this->load->model('Blog') - El Forum - 02-04-2010

[eluser]ignitian[/eluser]
Thanks for the help

I got this...

Array
(
[0] => Blog
)

2nd Edit: Echo code into constructor worked also my OS is Windows XP, i use WAMP.

C:\wamp\www\site1\index.php
C:\wamp\www\site1\application
C:\wamp\www\system


$this->load->model('Blog') - El Forum - 02-04-2010

[eluser]Michael Wales[/eluser]
What in the world... let's make sure we're on the same page - this function now looks like this:

Code:
function get_last_news()
    {
        $this->load->model('blog');
        $data['query'] = $this->blog->get_last_ten_entries();
        return $data['query'];
    }

And the filename is models/blog.php.

Correct?


$this->load->model('Blog') - El Forum - 02-04-2010

[eluser]ignitian[/eluser]
Exactly. include_path ??


$this->load->model('Blog') - El Forum - 02-04-2010

[eluser]Michael Wales[/eluser]
No, that shouldn't be the issue (unless your other projects have a blog model as well). CodeIgniter would throw an error saying it can't find the model you are referring to if you request one it can not find.

Just to make sure, let's open the index.php for this application and make sure the $application_folder setting is correct. Then, place the following bit of code in a controller somewhere so you can check the output:

Code:
echo APPPATH . 'models/' . $path . 'blog' . EXT;

If you follow that path on your filesystem it should lead you straight to the model which includes the get_last_ten_entries() method.


$this->load->model('Blog') - El Forum - 02-04-2010

[eluser]ignitian[/eluser]
Code:
echo APPPATH . 'models/' . $path . 'blog' . EXT;

My model is not in a subfolder. It is correct.

Code:
application/models/blog.php

I dont understand why this is not working. It's so simple what the...

Code:
http://localhost/site1/index.php/welcome/get_news