views:

27

answers:

2

Hello

I am a new bie to zend framework

I am getting an error while loading my index controller as

Fatal error: Class 'Places' not found in C:\xampp\htdocs\zend\book\application\controllers\IndexController.php on line 36

my bootstrapper code is

<?php
class Bootstrap
{
    public function __construct($configSection)
    {
        $rootDir = dirname(dirname(__FILE__));
        define('ROOT_DIR', $rootDir);
        set_include_path(get_include_path(). PATH_SEPARATOR . ROOT_DIR . '/library/'. PATH_SEPARATOR . ROOT_DIR .
        '/application/models/');

        require_once 'Zend/Loader/Autoloader.php';
        $loader = Zend_Loader_Autoloader::getInstance();
        // Load configuration
        Zend_Registry::set('configSection',$configSection);
        $config = new Zend_Config_Ini(ROOT_DIR.'/application/config.ini',$configSection);
        Zend_Registry::set('config', $config);
        date_default_timezone_set($config->date_default_timezone);
        // configure database and store to the registry
        $db = Zend_Db::factory($config->db);
        Zend_Db_Table_Abstract::setDefaultAdapter($db);
        Zend_Registry::set('db', $db);
    }

    public function configureFrontController()
    {
        $frontController = Zend_Controller_Front::getInstance();
        $frontController->setControllerDirectory(ROOT_DIR .'/application/controllers');
    }

    public function runApp()
    {
        $this->configureFrontController();
        // run!
        $frontController = Zend_Controller_Front::getInstance();
        $frontController->dispatch();
    }
}

I have a model as

<?php
class Places extends Zend_Db_Table
{
    protected $_name = 'places'; //table name
    function fetchLatest($count = 10)
    {
        return $this->fetchAll(null,'date_created DESC', $count);
    }   
}

and my index controller is

class IndexController extends Zend_Controller_Action
{
    public function indexAction()
    {
        $this->view->title = 'Welcome';
        $placesFinder = new Places();
        $this->view->places = $places->fetchLatest();
    }
}

I am using ZF version 1.10.4 what should I updated to make it work

Thanks a lot

A: 

There is a good chance you are missing somethign in your class declaration try:

   <?php
class Models_Places extends Zend_Db_Table
{
    protected $_name = 'places'; //table name
    function fetchLatest($count = 10)
    {
        return $this->fetchAll(null,'date_created DESC', $count);
    }   
}

The Zend autoloader class will look into Models/places.php for your class.

Also you could initialise the models and default module in bootstrap with:

protected function _initAutoload() {

        $autoloader = new Zend_Application_Module_Autoloader(array(
                    'namespace' => '',
                    'basePath' => dirname(__FILE__),
                ));
        $autoloader->addResourceType('models', 'models/', 'Models');
        return $autoloader;
    }

After having done that your class should be named Models_Places.

Check out the docs about autoloading.

Iznogood
Thank you, unfortunately it doesn't seems to be working with your code
john
Well what is not working? It should work unless you have a problem somewhere in your code.
Iznogood
showing same error, I think there is something missing on the $loader ?
john
I could solve this issue by adding include('Places.php'); at the top of the IndexController, Is there anyway to avoid this inclusion,I would like to load it automatically
john
Read what I linked about the autoloader. Normaly if you initialise it in bootstrap like I showed it should work unless your folders are organised differently.
Iznogood
A: 

Well, personally, I use extended controllers which contain few util methods I use very often. Here is a snippet of my extended controller:

<?php
class My_MyController extends Zend_Controller_Action
{
    protected $_tables = array();

    protected function _getTable($table)
    {
        if (false === array_key_exists($table, $this->_tables)) {
            include APPLICATION_PATH . '/modules/'
            . $this->_request->getModuleName() . '/models/' . $table . '.php';
            $this->_tables[$table] = new $table();
        }
        return $this->_tables[$table];
    }
}

You just need to define the APPLICATION_PATH in index.php. Then your controller could look like this:

<?php
class IndexController extends My_MyController
{
    public function indexAction()
    {
        // get model
        $model = $this->_getTable('ModelName');
    }
}

Path where you store the My_Controller must also be in your include path.

Richard Knop