• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Could not find Model

#1
[eluser]biber90[/eluser]
Hey Codeigniter Community,

today I stumbled about a very odd problem:
I have created a model called "test_model" with a function in it.

If I load this model in another models function, the function of the test_model works fine.

But when I want to connect to the database automatically (so I add 'database' to the autoload array), than
I get an error that the function in the test_model could not be find.

I can not appreciate it. Where is the connection between the database library and models?

Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: Calling_model::$test_model

Filename: models/calling_model.php

Line Number: 148

Fatal error: Call to a member function showTest() on a non-object in /Users/kevin/Coding/system/application/models/test_model.php on line 148

Thanks for your help!

#2
[eluser]danmontgomery[/eluser]
I don't understand... You're calling test_model as a member of another class from within the test_model class? You may need to post some code.

#3
[eluser]jedd[/eluser]
[quote author="biber90" date="1279213455"]
I have created a model called "test_model" with a function in it.
[/quote]

More information - what's the filename, what's the model's actual name.

Quote:If I load this model in another models function, the function of the test_model works fine.

Don't do this. It suggests your design is flawed. Consider extending the Model (MY_Model) or shifting common functionality into a library (or even helper - though if you're trying to do DB stuff, a library is preferable).

Quote:But when I want to connect to the database automatically (so I add 'database' to the autoload array), than
I get an error that the function in the test_model could not be find.

The error message you showed isn't the same as 'test_model could not be found'. Did you see other error messages that you didn't post?

Quote:I can not appreciate it. Where is the connection between the database library and models?

The user guide goes through this in some detail - I'd recommend you re-read the sections on libraries and models.

The [url="/wiki/FAQ"]FAQ[/url] also contains an answer to this question (okay - so I just put it in there - but only because I'm getting bored of the number of people who post questions without scanning the forums first for an existing answer. Have a read through my [url="/wiki/How_to_ask_a_good_question/"]guide to asking good questions[/url] when you have a minute.

#4
[eluser]biber90[/eluser]
Ok, i am using a model called 'buildings_model'.
In this model is a function called showAll();

Code:
<?php

class Buildings_model extends Model {

function showAll() {

}

}
?>

I also have another model called 'update_model'.

Code:
<?php

class Update_model extends Model {

function test() {

$this->buildings_model->showAll();

//do other stuff


}


}


?>

The controller loads the database, both models and calls the function test. Everything ok so far. But when I add the database to the autoload array, I get the error above.

#5
[eluser]jedd[/eluser]
[quote author="biber90" date="1279217079"]Ok, i am using a model called 'buildings_model'.
[/quote]

I'll ask again - what is/are the filename(s)?

Can you also quote the section of your autoload.php file where you're trying to autoload the model(s)?

Actually, what do you mean by 'auto-loading the database'?

Quote:
Code:
?>

Don't close PHP tags in your models or controllers.

#6
[eluser]biber90[/eluser]
The filenames are like the name of the classes (buildings_model.php, update_model.php).

The section of my autoload.php is:
$autoload['libraries'] = array('database', 'session');
I don't want to autoload the models. I only want to autoload the library database, without getting the above error.

#7
[eluser]jedd[/eluser]
[quote author="biber90" date="1279223769"]The filenames are like the name of the classes (buildings_model.php, update_model.php).
[/quote]

Good. I wanted to rule out case sensitivity issues.

Quote:The section of my autoload.php is:
$autoload['libraries'] = array('database', 'session');

This looks normal.

I don't think autoloading the database library is the root cause of your problem. The error cites line 148 - perhaps you could show us that region - of the file models/test_model.php.

The error seems to be coming from a different file, though - models/calling_model.php.

If you comment-out the loading of those two models, does your database library load okay?

#8
[eluser]biber90[/eluser]
I found the solution to my problem Smile
I included the models like this:

$this->load->model(array('update_model', 'buildings_model'));

But when the update_model gets initialized it can not find the function in the building_model. So i changed the order of them.
Another careless mistake, sorry!
And thanks for your help Smile Great forums.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.