Zend Framework 1.11 + Doctrine 2 Integration: Autoloading Models models/user.php -> Model\User
How can I setup autoloading such that I can have my directory structure like
/application
/models <<--- plural
...
And have Zend/Doctrine Autoloader load Application\Model\User
. Notice Model is singular.
Currently I have autoloader setup as follows (in Bootstrap.php _initDoctrine()
)
$zendAutoloader = Zend_Loader_Autoloader::getInstance();
// Symfony
$autoloader = array(new ClassLoader('Symfony'), 'loadClass');
$zendAutoloader->pushAutoloader($autoloader, 'Symfony\\');
// Doctrine
$autoload开发者_StackOverflower = array(new ClassLoader('Doctrine'), 'loadClass');
$zendAutoloader->pushAutoloader($autoloader, 'Doctrine\\');
// Models
$autoloader = array(new ClassLoader('Application\\Model', realpath(__DIR__ . '/models')), 'loadClass');
$zendAutoloader->pushAutoloader($autoloader, 'Application\\Model');
// Proxies
$autoloader = array(new ClassLoader('Application\\Model\\Proxy', realpath(__DIR__ . '/models/proxies')), 'loadClass');
$zendAutoloader->pushAutoloader($autoloader, 'Application\\Model\\Proxy');
Currently, when I try using Application\Model\User
, I get
require(D:\Projects\Tickle\application\models\Application\Model\User.php): failed to open stream: No such file or directory
Its trying to include application\models\Application\Model\User.php
. hmm its wierder than I expected. How can I fix it anyways?
Try:
$modelsClassLoader = new ClassLoader('Application\Model', __DIR__ . '/models');
$modelsClassLoader->register();
The way I resolved this was to use Doctrine's autoloader/ClassLoader
instead of Zend's
// disable Zend Autoloader
spl_autoload_unregister(array('Zend_Loader_Autoloader','autoload'));
// use Doctrine2's Class Loader
$autoloader = new ClassLoader('Zend');
$autoloader->setNamespaceSeparator('_');
$autoloader->register();
// Symfony
$autoloader = new ClassLoader('Symfony');
$autoloader->register();
// Doctrine
$autoloader = new ClassLoader('Doctrine');
$autoloader->register();
// Application
$autoloader = new ClassLoader('Application', realpath(__DIR__ . '/..'));
$autoloader->register();
1 of my mistakes was that I should be using realpath(__DIR__ . '/..')
which with my directory structure, ...
/Tickle (Project name)
/application
/models
/proxies
...
Points to "Tickle", so that when doctrine appends the path "Application/Models/User.php" it will look like "/Tickle/Application/Models/Users.php" which exists
精彩评论