开发者

Doctrine lazy loading

开发者 https://www.devze.com 2023-01-14 15:53 出处:网络
I\'m just getting to grips with Doctrine, and using the suggested lazy loading of models. As per the tutorials, I\'ve created a doctrine bootstrap file:

I'm just getting to grips with Doctrine, and using the suggested lazy loading of models. As per the tutorials, I've 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::load开发者_JAVA技巧Models(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 have all been created by Doctrine.

I've 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 generates 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 any 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 auto loading the models correctly. What am I missing?


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

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

And then auto loading works as expected


Your approach is correct since Doctrine has it's own loading functionallity:

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

This is not recursive so you need to add folders that contain your mapped/managed models.


In the User.php model there needs to be a require to the BaseUser.php class at the top. As the user class extends the BaseUser.php

I have had this issue and that has solved it. I would be interested if there is something I am missing to not have to do that include manually. Give that a shot and see if it fixes the issue without having to require the User.php

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号