views:

1094

answers:

3

i am following the tutorial from http://www.zendcasts.com/introducing-doctrine-1-2-integration/2009/11/

i have a doctrine.php that "bootstraps?" doctrine ... sry i dont really fully understand the tut yet

// Define path to application directory
defined('APPLICATION_PATH')
    || define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/..'));

// Define application environment
defined('APPLICATION_ENV')
    || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));

// Ensure library/ is on include_path
set_include_path(implode(PATH_SEPARATOR, array(
    realpath(APPLICATION_PATH . '/../library'),
    get_include_path(),
)));

/** Zend_Application */
require_once 'Zend/Application.php';

// Create application, bootstrap, and run
$application = new Zend_Application(
    APPLICATION_ENV,
    APPLICATION_PATH . '/configs/application.ini'
);

$application->getBootstrap()->bootstrap('doctrine');
$config = $application->getOption('doctrine');

$cli = new Doctrine_Cli($config);
$cli->run($_SERVER['argv']);

when i try to run it via cmd,

php.exe doctrine.php

i get

D:\Projects\ZF\doctrine\application\scripts>php -f doctrine.php
PHP Fatal error:  Uncaught exception 'LogicException' with message 'Passed array does not specify an existing static met
hod (class 'Doctrine' not found)' in D:\Projects\ZF\doctrine\application\Bootstrap.php:7
Stack trace:
#0 D:\Projects\ZF\doctrine\application\Bootstrap.php(7): spl_autoload_register(Array)
#1 D:\ResourceLibrary\Frameworks\ZendFramework\library\Zend\Application\Bootstrap\BootstrapAbstract.php(662): Bootstrap-
>_initDoctrine()
#2 D:\ResourceLibrary\Frameworks\ZendFramework\library\Zend\Application\Bootstrap\BootstrapAbstract.php(622): Zend_Appli
cation_Bootstrap_BootstrapAbstract->_executeResource('doctrine')
#3 D:\ResourceLibrary\Frameworks\ZendFramework\library\Zend\Application\Bootstrap\BootstrapAbstract.php(579): Zend_Appli
cation_Bootstrap_BootstrapAbstract->_bootstrap('doctrine')
#4 D:\Projects\ZF\doctrine\application\scripts\doctrine.php(25): Zend_Application_Bootstrap_BootstrapAbstract->bootstrap
('doctrine')
#5 D:\ResourceLibrary\Frameworks\ZendFramework\library\Zend\Loader.php(136): include('D:\Projects\ZF in D:\Projects\ZF\d
octrine\application\Bootstrap.php on line 0

.

UPDATE 1

.

protected function _initDoctrine() {
    $this->getApplication()->getAutoloader()
         ->pushAutoloader(array('Doctrine', 'autoload'));
    spl_autoload_register(array('Doctrine', 'modelsAutoload'));

    $manager = Doctrine_Manager::getInstance();
    $manager->setAttribute(Doctrine::ATTR_AUTO_ACCESSOR_OVERRIDE, true);
    $manager->setAttribute(Doctrine::ATTR_MODEL_LOADING, Doctrine::MODEL_LOADING_CONSERVATIVE);
    $manager->setAttribute(Doctrine::ATTR_AUTOLOAD_TABLE_CLASSES, true);

    $doctrineConfig = $this->getOption("doctrine");
    $conn = Doctrine_Manager::connection($doctrineConfig['dsn']);
    $conn->setAttribute(Doctrine::ATTR_USE_NATIVE_ENUM, true);
    return $conn;
}

under sql_autoload_register() i guess. i dont really get what spl_autoload_register() also ... even in php reference

UPDATE 2

my bootstrap.php

<?php
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap {
    protected function _initDoctrine() {
        $this->getApplication()->getAutoloader()
                ->pushAutoloader(array('Doctrine', 'autoload'));
        spl_autoload_register(array('Doctrine', 'modelsAutoload'));

        $manager = Doctrine_Manager::getInstance();
        $manager->setAttribute(Doctrine::ATTR_AUTO_ACCESSOR_OVERRIDE, true);
        $manager->setAttribute(Doctrine::ATTR_MODEL_LOADING, Doctrine::MODEL_LOADING_CONSERVATIVE);
        $manager->setAttribute(Doctrine::ATTR_AUTOLOAD_TABLE_CLASSES, true);

        $doctrineConfig = $this->getOption("doctrine");
        Doctrine::loadModels($doctrineConfig['models_path']);

        $conn = Doctrine_Manager::connection($doctrineConfig['dsn'], 'Doctrine');
        $conn->setAttribute(Doctrine::ATTR_USE_NATIVE_ENUM, true);
        return $conn;
    }
}

i guess the thing thats not working is Zend does not seem to autoload the Doctrine class. which its supposed to cos i already have registered the Doctrine namespace in config.ini

autoloaderNamespaces[] = "Doctrine"

does this mean Zend shld autoload Doctrine classes?

as for where Doctrine is stored, i have pointed to it inside PHP's include path.

include_path = ".;c:\php\includes;D:\ResourceLibrary\Frameworks\ZendFramework\library;D:\ResourceLibrary\Frameworks\Doctrine121sandbox\lib"

i noted that if in _initDoctrine(), i require Doctrine manually it works.

require_once 'D:\ResourceLibrary\Frameworks\Doctrine121sandbox\lib\Doctrine.php';

has it got something to do with windows path (\ as separator)

A: 

Do you have the rest of the Zend Framework stuff working without the use of Doctrine? If so, you have a bootstrap file that you use for that application. Assuming a default directory layout, if you have your doctrine.php in your scripts directory and your bootstrap.php in your application directory, you should be able to just do this to get your doctrine cli working in doctrine.php:

require dirname(__FILE__).'/../application/global.php';
// the config line below might be different based on how you have your
// doctrine config stuff set up
$cli = new Doctrine_Cli(Zend_Registry::get('doctrine_config'));
$cli->run($_SERVER['argv']);

In your bootstrap.php (or a file required by your bootstrap.php), your autoloader should be configured something like this:

// set up your include path here
set_include_path(dirname(__FILE__).'/../library/zendframework'
. PATH_SEPARATOR . dirname(__FILE__).'/../library/doctrine'
. PATH_SEPARATOR . dirname(__FILE__).'/models'
. PATH_SEPARATOR . dirname(__FILE__).'/models/generated'
. PATH_SEPARATOR . dirname(__FILE__).'/business'
. PATH_SEPARATOR . dirname(__FILE__).'/business/exceptions'
. PATH_SEPARATOR . dirname(__FILE__).'/business/util'
. PATH_SEPARATOR . dirname(__FILE__).'/business/validators'
. PATH_SEPARATOR . get_include_path());
require 'Zend/Loader/Autoloader.php';

$loader = Zend_Loader_Autoloader::getInstance();
$loader->suppressNotFoundWarnings(false);
$loader->setFallbackAutoloader(true);

Post here if this doesn't work. We had a bunch of issues setting this up in the beginning with Zend Framework's autoloader.

Chris Williams
yes, i have rest of ZF working. but what is global.php? it seems like it pointing to the code from Bootstrap.php _initDoctrine() - refer to update 1
iceangel89
Oops. Forgot to mention that. What we do is that we have a bootstrap.php and on the first line of that file, we require our global.php (in the same location as bootstrap.php). Almost all of our logic that is independent of the MVC stuff is on global.php. This is so other code can use global to setup the autoloader, for example. The MVC stuff is in bootstrap.php (stuff like: Zend_Layout::startMvc(APPLICATION_PATH . '/layouts/scripts') and Zend_Controller_Front::run(dirname(__FILE__).'/controllers'))..
Chris Williams
it seems it cannot find/autoload the Doctrine class. i have autoloaderNamespaces[] = "Doctrine" in application.ini already. and i am using Doctrine 1.2 does it makes any difference?
iceangel89
i also noticed that if i put the doctrine library inside my library folder in my project rather than in a shared folder and include that in php.ini, it will work.
iceangel89
So how are you including the files related to doctrine? Are you adding the doctrine directory to the include path? Are you using an absolute or relative path? Can you post that code?
Chris Williams
i am doing it in php.ini include_path = ".;c:\php\includes;D:\ResourceLibrary\Frameworks\ZendFramework\library;**D:\ResourceLibrary\Frameworks\Doctrine121sandbox\lib**"
iceangel89
I've added the include path to the code in my answer. Are you using the Zend Autoloader in your bootstrap file?
Chris Williams
see update 2. i guess i am using Zend Autoloader?
iceangel89
A: 

I did face the same problem and got over it successfully what you need to do is change this

    $manager->setAttribute(Doctrine::ATTR_AUTOLOAD_TABLE_CLASSES, true);

to

    $manager->setAttribute(Doctrine::ATTR_AUTOLOAD_TABLE_CLASSES, false);

and i did remove this line also

  spl_autoload_register(array('Doctrine', 'modelsAutoload'));

I had reported these errors in the same zendcasts forums
these steps helped me to work with doctrine as expected :)

tawfekov
A: 

I suggest you to go by this tutorial and fast and easy method : http://zend-framework-doctrine.blogspot.com/

Sameer

sameer