Ayrx
Ayrx

Reputation: 2162

Zend Framework - How does a controller point to a model?

This is a problem that I cannot figure out after hours of googling, the Zend Framework documentation didn't explain what i needed to know.

I have a file: /application/controllers/RegisterController.php

class RegisterController extends Zend_Controller_Action
{
}

How do I point this file to use classes from: /application/models/User.php

class Application_Model_User
{
}

The code works great when the file is named Register.php

What configurations do i need to make to point a controller to a specific model?

Upvotes: 0

Views: 87

Answers (1)

Joel Lord
Joel Lord

Reputation: 2173

In your bootstrap, you can start by declaring your namespace:

protected function _initAutoload() {
    //Autoloader
    $autoLoader = Zend_Loader_Autoloader::getInstance();
    $resourceLoader = new Zend_Loader_Autoloader_Resource(array(
            'basePath' => APPLICATION_PATH,
            'namespace' => '',
            'resourceTypes' => array(

                    //Tells the application where to find the models
                    'model' => array(
                            'path' => 'models/',
                            'namespace' => 'Model_'
                            )
                    )
        ));

    return $autoLoader;
}

Your model: /application/models/User.php

class Model_User extends Zend_Db_Table_Abstract 
{
}

Then in you controller: /application/controllers/RegisterController.php

class RegisterController extends Zend_Controller_Action
{
public function indexAction() {
$model = new Model_User();
}
}

Upvotes: 4

Related Questions