Doctrine offers two ways of loading models. We have conservative(lazy) loading, and aggressive loading. Conservative loading will not require the PHP file initially, instead it will cache the path to the class name and this path is then used in the Doctrine_Core::modelsAutoload().
To use Doctrine model loading you need to register the model autoloader in your
bootstrap:
// bootstrap.php
// ...
spl_autoload_register(array('Doctrine_Core', 'modelsAutoload'));
Below are some examples using the both types of model loading.
Conservative model loading is going to be the ideal model loading method for a production environment. This method will lazy load all of the models instead of loading them all when model loading is executed.
Conservative model loading requires that each file contain only one class, and the file must be named after the class. For example, if you have a class named User, it must be contained in a file named User.php.
To use conservative model loading we need to set the model loading attribute to be conservative:
$manager->setAttribute(Doctrine_Core::ATTR_MODEL_LOADING, Doctrine_Core::MODEL_LOADING_CONSERVATIVE);
We already made this change in an earlier step in the bootstrap.php file so you don't need to make this change again.
When we use the Doctrine_Core::loadModels() functionality all found classes will be cached internally so the autoloader can require them later.
Doctrine_Core::loadModels('models');
Now when we instantiate a new class, for example a User class, the autoloader will be triggered and the class is required.
// triggers call to Doctrine_Core::modelsAutoload() and the class is included
$user = new User();
Instantiating the class above triggers a call to Doctrine_Core::modelsAutoload() and the class that was found in the call to Doctrine_Core::loadModels() will be required and made available.
Conservative model loading is recommended in most cases, specifically for production environments as you do not want to require every single model class even when it is not needed as this is unnecessary overhead. You only want to require it when it is needed.
Aggressive model loading is the default model loading method and is very simple, it will look for all files with a .php extension and will include it. Doctrine can not satisfy any inheritance and if your models extend another model, it cannot include them in the correct order so it is up to you to make sure all dependencies are satisfied in each class.
With aggressive model loading you can have multiple classes per file and the file name is not required to be related to the name of the class inside of the file.
The downside of aggressive model loading is that every php file is included in every request, so if you have lots of models it is recommended you use conservative model loading.
To use aggressive model loading we need to set the model loading attribute to be aggressive:
$manager->setAttribute(Doctrine_Core::ATTR_MODEL_LOADING, Doctrine_Core::MODEL_LOADING_AGGRESSIVE);
Aggressive is the default of the model loading attribute so explicitly setting it is not necessary if you wish to use it.
When we use the Doctrine_Core::loadModels() functionality all the classes found will be included right away:
Doctrine_Core::loadModels('/path/to/models');