0

I really can't figure this out:

I've created a file called User.php in application/models. The classname in it is Model_User.

When I try to create an object in my Controller, I get this error:

Fatal error: Class 'Model_User' not found in C:\xampplite\htdocs\code\application\controllers\IndexController.php on line 14

I googled around, and found this code, which is supposed to autoload controllers for me, it is located in bootstrap.php The code isn't working though. The example that used this code was working with ZF 1.8 so that might be the reason but I can't figure it out. How should I autoload my models?!

    private function _initAutoload(){
 $modelLoader = new Zend_Application_Module_Autoloader(array(
     'namespace' => '',
     'basePath' => APPLICATION_PATH
 ));
 return $modelLoader;
    }

Any ideas?

2
  • 1
    Possible duplicate of Use Zend_Autoloader for Models. Commented Dec 15, 2010 at 0:27
  • I think we're both using the code from the same tutorial, but he says it's working for him except for subdirectories. I can't get it to work at all. Commented Dec 15, 2010 at 0:30

1 Answer 1

1

The important bit in the answer to the question I linked above is the namespace:

    $resourceLoader->addResourceTypes(array(
            'model' => array(
                    'namespace' => 'Model',
                    'path'      => 'models'
            )
    ));

The namespace parameter tells the Autoloader to look in the defined path (relative to basePath) when encountering a class that starts with Model_. You have the first part right, but you're missing the call to addResourceTypes.

Sign up to request clarification or add additional context in comments.

2 Comments

That doesn't work either. (I did change the object name, it's not a typo) Sorry I misunderstood earlier!
It seems I needed an insctane from the Zend_Loader_Autoloader_Resource class, and not the Zend_Application_Module_Autoloader which I was using. Fixed!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.