Model function references not recognized

#1
[eluser]Frank Cole[/eluser]
Hi all,

Looking for some help here. After my hosting provider upgraded to PHP 5.2.5, Models are no longer working as expected.

I made a fresh install of CI and modified the simple welcome script that comes with it to demonstrate:

Contorller: welcome.php
Code:
<?php

class Welcome extends Controller {

        function Welcome()
        {
                parent::Controller();
        }

        function index()
        {
                $this->load->view('welcome_message');
                $this->load->model('Welcome_model');
                $this->Welcome_model->helloWorld();   // The Model error occurs here...
        }
}
?>

Model: welcome_model.php
Code:
<?php

class Welcome_model extends Model
{
        function Welcome_model()
        {
                parent::Model();
        }

        function helloWorld()
        {
                echo "Hello World!";
        }
}
?>

Error: from CI and PHP debug
Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined property: Welcome::$Welcome_model

Filename: controllers/welcome.php

Line Number: 14

Fatal error: Call to a member function helloWorld() on a non-object in /www/htdocs/system/application/controllers/welcome.php on line 14

PHP Version 5.2.5
CI Version 1.5.4

Please somebody tell me there is some really simple fix to this.....

Frank

#2
[eluser]Derek Allard[/eluser]
Hi Frank. Welcome to CodeIgniter.

I think its just a matter of getting comfortable with how models work for you. Try this. In your model, change it from "echo" to "return", which just means "hand this off to something else".

Now I'm just going to flip the order of what you've got here a bit. I'm going to load the model first (you need to load it before you reference it), and then I'm going to store the results of a the hello world function into something I can pass to the view, in this case an array called "data".

Code:
<?php

class Welcome extends Controller {

        function Welcome()
        {
                parent::Controller();
        }

        function index()
        {
                $this->load->model('Welcome_model');
                // notice how I'm storing this as "my_message" in an array called data?
                $data['my_message'] = $this->Welcome_model->helloWorld();
                // now when we load the view, we make sure the data array goes into it
                $this->load->view('welcome_message', $data);
        }
}
?>

Now in your welcome_message view, put this
Code:
<?php echo $my_message;?>

#3
[eluser]coolfactor[/eluser]
Frank, could it be a case-sensitivity issue?

Do you see welcome_model loaded when you add the following line?

Code:
function index()
        {
                $this->load->view('welcome_message');
                $this->load->model('Welcome_model');

                var_dump($this);  // <----- add this for debugging

                $this->Welcome_model->helloWorld();   // The Model error occurs here...
        }

#4
[eluser]Frank Cole[/eluser]
Hi guys,

Thanks for the replies. I tried all that stuff before I posted.
Turns out it was a PHP issue that affects more than just Models. I found the answer in this thread http://ellislab.com/forums/viewthread/53380/ in the last entry.

Quote:I have found out that the zend.ze1_compatibility_mode in the php.ini should be disabled.

Fortunately I have access to my php.ini. For others that arent so lucky it can also be added to the .htaccess file. It should be set to "Off". Once I set it to off, it fixed several other issues I was having as well.

Please consider adding this in the docs or the FAQ somewhere.

Again Thanks for the replies and Happy New Year....

Frank

PS -- Oh and yes, I know, you shouldnt send stuff from a Model. It was just a quick way of demonstrating my problem.

#5
[eluser]Phil Norton[/eluser]
Not sure if I'm having the same problem. Basically, I want to migrate a site I've built in CI (http://dev.andersonarchitect.com) from one server to a Media Temple server. I've uploaded the files, and made the relevant changes to the configs, but when I try to view the site on the new server, I get:

Fatal error: Call to a member function on a non-object in /system/application/controllers/site.php on line 12

Thing is, that error isn't appearing on my other server. And line 12 merely does this:

Code:
$nav = $this->site_model->listNav();

And yes, the model is being loaded in the controller. So, why would this work on one server, and not the next? Not sure if it's a PHP error, as my current server is running PHP5, and Media Temple are running PHP4, but this really is baffling me. I tried the var_dump thing, and I could see no sign of the site_model being loaded. So it's as if CI isn't loading the model, even when I tell it to:

Code:
function __construct()
    {
        parent::Controller();
        $this->load->model('site_model', '', TRUE);
    }

Can anyone suggest anything that might help? Bizarrely, there's an admin section that seems to pretty much be working fine, though it's referencing a different model, and uses a different controller.

Help me CI Forum Dwellers, you're my only hope!

Phil

#6
[eluser]Frank Cole[/eluser]
PHP 4 doesnt use __construct() for class constructors, its a PHP 5 only thing, so youll have to go in and adjust any classes that do it this way to work with PHP 4. Basically you'll have to go in and undo any other PHP5isms as well.

So it should be something like:

Code:
class Site_model extends Model
{
     function Site_model()
     {
          parent::Model();
     }

     function listNav()
     {
          // Do whatever
     }

}

Hope it helps.

Frank

#7
[eluser]coolfactor[/eluser]
I believe if you switch your index file to "index.php5" on your Media Temple server, it'll use PHP 5 instead of PHP 4. That's how it was for a site I worked on a few month ago hosted with MT.

#8
[eluser]Phil Norton[/eluser]
Cheers Frank, you're a star! That did the trick. I'm not much of a programmer (which is why CI and Expression Engine are a God-send) so I've cobbled together the code from different tutorials, so that must be where the PHP5 stuff crept in!

Much appreciated,
Phil


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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