Welcome Guest, Not a member yet? Register   Sign In
Problem Loading MY_Model
#1

[eluser]kirkaracha[/eluser]
I'm running multiple applications under CodeIgniter 1.7.2 based on the user guide instructions, and I'm having trouble loading an extended model. I'm autoloading the Active Record and Database libraries. Here's my trimmed-down directory structure:

/codeigniter/ <- renamed "system"
/codeigniter/application/my_application/controllers/test.php <- controller
/codeigniter/application/my_application/models/test_model.php <- model
/codeigniter/application/my_application/libraries/MY_Model.php <- extended model

Here's test.php:
Code:
&lt;?php
class Test extends Controller {

    function Test() {
        parent::Controller();

        // load models
        $this->load->model('Test_model');
    }


    function index() {
        $data = array(
            'page_title' => 'Testing.',
            'message_text' => 'Testing 1-2-3.'
        );
        $data['templates'] = array(
            'shared/header',
            'shared/display_messages',
            'shared/footer'
        );
        $this->load->view('shared/display',$data);
    } // index


} // Test
?&gt;

Here's test_model.php:
Code:
&lt;?php

class Test_model extends MY_Model{

    function Test_model() {
        parent::MY_Model();
    }


} // Test_model

Here's MY_Model.php:
Code:
&lt;?php

class MY_Model extends Model {

    function Model(){
        parent::Model();
    }

} // MY_Model

The test controller works fine if I go to it without loading any models, and if I load the test model without trying to load the extended model. If I try to load the extended model, I get this error:

"Fatal error: Class 'MY_Model' not found in /codeigniter/application/my_application/models/test_model.php on line 3"

What am I doing wrong?

Bonus question: could I move MY_Model.php to /codeigniter/libraries/ and use it in all my applications?
#2

[eluser]imn.codeartist[/eluser]
Check your code

Code:
&lt;?php

class MY_Model extends Model {

    function Model(){ // Mistake is here suppose to be MY_Model not Model
        parent::Model();
    }

} // MY_Model
#3

[eluser]John_Betong[/eluser]
&nbsp;

Try this:
Code:
&lt;?php
   // hopefully MY_Model.php is in the directory. BEWARE it is case-sensitive
   require(APPPATH.'libraries/MY_Model.php');

class MY_Model extends Model {

   /* Maybe Not required */
    function MY_Model(){
        parent::Model();
    }

   function test()
   {
     echo 'MY_Model.php ==> ' .__FILE__;
   }
  

} // MY_Model
&nbsp;
&nbsp;
&nbsp;
edit: Renamed function to MY_Model
#4

[eluser]Phil Sturgeon[/eluser]
Strange error there, that should work.

Quote:Bonus question: could I move MY_Model.php to /codeigniter/libraries/ and use it in all my applications?

That should work fine. I would suggest moving it anyway, might solve this weird error.




Theme © iAndrew 2016 - Forum software by © MyBB