0
votes

Iam writing an application using Zend Framework 1.10.2.

I created few model classes and a controller to process them.

When Iam executing my application and accessing the admin controller. Iam seeing this error.

Fatal error: Class 'Application_Model_DbTable_Users' not found in C:\xampp\htdocs\bidpopo\application\controllers\AdminController.php on line 16

The error clearly shows its an autoloading error.

Hence I wrote this code in the bootstrap file.

    protected function initAutoload()
{
    $modeLoader = new Zend_Application_Module_AutoLoader(array
        ('namespace'=>'','basePath'=>APPLICATION_PATH ));
    //echo(APPLICATION_PATH);
    return $modeLoader;
}

Still the error remains :( . Can anyone suggest me what Iam missing out here?

This is the location of the Model Users class.

C:\xampp\htdocs\bidpopo\application\models\DbTable\Users.php

This is its code.

    class Application_Model_DbTable_Users extends Zend_Db_Table_Abstract
{
    //put your code here
protected $_name='users';

public function getUser($id)
{
    $id = (int)$id;
    $row = $this->fetchrow('id='.$id);
    if(!$row)
    {throw new Exception("Could not find row id - $id");}
    return $row->toArray();   
}

public function addUser($userDetailArray)
{
    $this->insert($userDetailsArray);
}

public function updateUser($id,$userDetailArray)
{
    $this->update($userDetailArray,'id='.(int)$id);
}

public function deleteUser($id)
{
    $this->delete('id='. (int)$id);
}

}

This is the Admin Controller's code

class AdminController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $this->view->title= "All Users";
        $this->view->headTitle($this->view->title);

        $users = new Application_Model_DbTable_Users();
        $this->view->users = $users->fetchAll();
    }

    public function addUserAction()
    {
        // action body
    }

    public function editUserAction()
    {
        // action body
    }

    public function deleteUserAction()
    {
        // action body
    }
1

1 Answers

1
votes

You application classes don't follow the proper naming convention for the namespace you've set. The Zend_Application_Module_AutoLoader is a little different than the normal autoloader in that it doesn't simply change the '_' in the class name with '/'. It looks at the second part of the class name and then checks a folder for the existence of the class based on that.

You need to change the line:

$modeLoader = new   Zend_Application_Module_AutoLoader(array(
    'namespace'=>'Application',
    'basePath'=>APPLICATION_PATH
));

This means it will autoload all module classes prefixed with 'Application_'. When it the second part of the class is 'Model_' it will look in "{$basePath}/models" for the class. The '_' in the rest of the class name will be replaced with '/'. So the file path of the file will be "{$basePath}/models/DbTable/Users.php".

Read more here.