Customize Doctrine model builder options
By eNk` on Thursday, January 21 2010, 14:51 - Symfony - Permalink
This is just a little tip to customize the builder options for the generated model classes (lib/model/). Basically if we define a MyClass class in the schema.yml we will have this:
- MyClass > BaseMyClass > sfDoctrineRecord > Doctrine_Record > ...
- MyClassTable > Doctrine_Table > ...
So the idea is to get somthing like:
- MyClass > BaseMyClass > MyDoctrineRecord > sfDoctrineRecord > Doctrine_Record > ...
- MyClassTable > MyDoctrineTable > Doctrine_Table > ...
This can be useful if we need to add common elements to all record or table objects in a project.
When you run a build model the task load the sfDoctrinePlugin default configuration. You can find this default configuration in sfDoctrinePluginConfiguration::getModelBuilderOptions().
This is the code from getModelBuilderOptions() (here I'm using sf1.3):
public function getModelBuilderOptions()
{
$options = array(
'generateBaseClasses' => true,
'generateTableClasses' => true,
'packagesPrefix' => 'Plugin',
'suffix' => '.class.php',
'baseClassesDirectory' => 'base',
'baseClassName' => 'sfDoctrineRecord',
);
// for BC
$options = array_merge($options, sfConfig::get('doctrine_model_builder_options', array()));
// filter options through the dispatcher
$options = $this->dispatcher->filter(new sfEvent($this, 'doctrine.filter_model_builder_options'), $options)->getReturnValue();
return $options;
}
As you can see $options contain the default configuration, this config is merged with sfConfig::get('doctrine_model_builder_options', array()), so we can easily customize this configuration by setting doctrine_model_builder_options in the ProjectConfiguration class like this:
class ProjectConfiguration extends sfProjectConfiguration
{
public function setup()
{
// ...
$this->setupCorePluginsCustomConfig();
}
public function setupCorePluginsCustomConfig()
{
// custom builder options for doctrine
sfConfig::set('doctrine_model_builder_options',
array('baseTableClassName' => 'MyDoctrineTable',
'baseClassName' => 'MyDoctrineRecord'));
}
}
MyDoctrineTable and MyDoctrineRecord simply extends Doctrine_Table and sfDoctrineRecord.
class MyDoctrineTable extends Doctrine_Table
{
// ...
}
class MyDoctrineRecord extends sfDoctrineRecord
{
// ...
}
Now if we build model base model files (for record objects in lib/model/base) will be generated with 'extends MyDoctrineRecord'. For table objects, if the files don't exist they will be created with 'extends MyDoctrineTable' else you will have to update it manually.
You can have a look to the Doctrine_Import_Builder class attributes to see all options.
Comments
<A href="http://www.toptoys2trade.com/zhu-zh... ">zhu zhu pets</A>
<a href="http://www.toptoys2trade.com/a-sill... "> silly bandz</a>
<A href="http://www.toptoys2trade.com/power-... "> power balance</A>
<A href="http://www.toptoys2trade.com/animal... ">animal rubber bands</A>
<A href="http://www.toptoys2trade.com/baby-c... ">Baby Carriers</A>
<A href="http://www.toptoys2trade.com/pillow... ">Pillow Pets</A>
<A href="http://www.toptoys2trade.com/plush-... ">Plush Pencil Case</A>
<A href="http://www.golf-equipment2u.com ">golf equipments</A>