Doctrine lazy loading

I just deal with the Doctrine and use the proposed lazy loading of models. In accordance with the tutorials, I created a doctrine bootstrap file:

<?php
require_once(dirname(__FILE__) . '/libs/doctrine/lib/Doctrine.php');
spl_autoload_register(array('Doctrine', 'autoload'));
$manager = Doctrine_Manager::getInstance();
$manager->setAttribute(Doctrine_Core::ATTR_AUTO_ACCESSOR_OVERRIDE, true);
$manager->setAttribute(Doctrine_Core::ATTR_MODEL_LOADING, Doctrine_Core::MODEL_LOADING_CONSERVATIVE);
Doctrine_Core::loadModels(array(dirname(__FILE__) . '/models/generated', dirname(__FILE__) . '/models')); //this line should apparently cause the Base classes to be loaded beforehand

My models and base classes were created by Doctrine.

I also created a simple test file as follows:

<?php
require_once('doctrine_bootstrap.php');

$user = new User();
$user->email = 'test@test.com';
echo $user->email;

However, this causes the following error:

Fatal error: Class 'User' not found in E:\xampp\htdocs\apnew\services\doctrine_test.php on line 4

However, if I explicitly require the BaseUser.php and User.php files, then it works fine without errors

<?php
require_once('doctrine_bootstrap.php');
require_once('models/generated/BaseUser.php');
require_once('models/User.php');

$user = new User();
$user->email = 'test@test.com';
echo $user->email;

So, it seems that Doctine is not automatically loading models correctly. What am I missing?

+5
source share
3 answers

OK, so you need the following line in the bootstrap file:

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

And then autoload works as expected

+2
source

, :

Doctrine::loadModels('models'); 
Doctrine::loadModels('models/generated'); 
Doctrine::loadModels('models/tables'); 
...

, , / .

+2

The User.php model requires a requirement for the BaseUser.php class at the top. As the user class extendsBaseUser.php

I had this problem and it solved it. I would be interested if there is something that I do not see, so that I do not need to do this manually. Give this snapshot and see if it fixes the problem without requiringUser.php

0
source

All Articles