Welcome Guest, Not a member yet? Register   Sign In
Autoloading Models PHP 5 Style
#1

[eluser]gcarrion[/eluser]
Hi,

I found this article posted over at Nettuts with pretty neat hacks:
http://net.tutsplus.com/tutorials/php/6-...e-masters/

I would like to use "hack" #1 (specially for the auto-complete feature in the IDE and the simplicity to call models), but is not working for me. I keep getting errors when running the page.

I added the required code to the end of application/config/config.php

Code:
function __autoload($class) {
    if (file_exists(APPPATH."models/".strtolower($class).EXT)) {
        include_once(APPPATH."models/".strtolower($class).EXT);
    } else if (file_exists(APPPATH."controllers/".strtolower($class).EXT)) {
        include_once(APPPATH."controllers/".strtolower($class).EXT);
    }
}


Then created a test controller: application/controllers/test.php

Code:
<?php
Class Test extends Controller {

    function index() {
        $test_model = New Test_model;
        $db_array = $test_model->getTest();
        print_r($db_array);
    }
}
?>

I then created a test model: application/models/test_model.php

Code:
<?php
class Test_model extends Model {

    function getTest() {
        $query = $this->db->query('select * from table where PostStatus = 1');

        if($query->num_rows() > 0) {

            foreach ($query->result() as $row) {
                $data[] = $row;
            }
        }
        return $data;
    }

}
?>

When I run the URL: mysite.com/test/
I get this message:

Quote:Fatal error: Class 'Model' not found in /path/to/web/application/models/test_model.php on line 2


Tried removing the "extends Model" from the test_model.php to be like this:
Code:
class Test_model {

Then ran it again and got this error:

Quote:A PHP Error was encountered
Severity: Notice
Message: Undefined property: Test_model::$db
Filename: models/test_model.php
Line Number: 5

Fatal error: Call to a member function query() on a non-object in /path/to/web/application/models/test_model.php on line 5


However, If I call the model the original way in the controller, it works fine.

Code:
$this->load->model('test_model');
$db_array = $this->test_model->getTest();


Any ideas of what I'm doing wrong?

Thanks!


Messages In This Thread
Autoloading Models PHP 5 Style - by El Forum - 12-29-2009, 05:36 PM
Autoloading Models PHP 5 Style - by El Forum - 12-29-2009, 07:49 PM
Autoloading Models PHP 5 Style - by El Forum - 12-29-2009, 08:52 PM
Autoloading Models PHP 5 Style - by El Forum - 12-29-2009, 08:57 PM
Autoloading Models PHP 5 Style - by El Forum - 12-29-2009, 09:17 PM
Autoloading Models PHP 5 Style - by El Forum - 12-30-2009, 12:42 AM
Autoloading Models PHP 5 Style - by El Forum - 12-30-2009, 03:05 AM
Autoloading Models PHP 5 Style - by El Forum - 12-30-2009, 04:39 PM
Autoloading Models PHP 5 Style - by El Forum - 12-31-2009, 06:27 AM
Autoloading Models PHP 5 Style - by El Forum - 01-01-2010, 09:10 AM
Autoloading Models PHP 5 Style - by El Forum - 01-01-2010, 09:30 AM



Theme © iAndrew 2016 - Forum software by © MyBB