Kohana 3.0.4.2 playing nice with Doctrine 1.2.2
Just been setting up Kohana 3 so that it plays nice with Doctrine 1.2.2, its a pretty simple process, but requires some minor changes to Doctrine_Core and Doctrine_Export.
You can download the Kohana module from here its just a matter of dumping that into your modules directory and configuring the init.php file.
Once you’ve done that you need to make some changes to your Doctrine lib, as you know all models within Kohana need to be prefixed with ‘Model_’ so your User model needs to be called ‘User.php’ but the class within it needs to have ‘Class Model_User extends…’ the problem here is that ‘Doctrine::createTablesFromModels’ only looks at the filename when creating a list of tables to make, so we need to tweak Doctrine to allow us to parse in the ‘Model_’.
If you open ‘Doctrine1/lib/Doctrine/Core.php’ and goto line 902 you’ll see this:
public static function createTablesFromModels($directory = null)
{
return Doctrine_Manager::connection()->export->exportSchema($directory);
}
If you replace that with:
public static function createTablesFromModels($directory = null, $classPrefix = null)
{
return Doctrine_Manager::connection()->export->exportSchema($directory, $classPrefix);
}
Basically this lets up parse a classPrefix into the exportSchema, which is needed for Kohana.
Next go to line 666 where you will see the following:
if ($classPrefix) {
$className = $classPrefix . $className;
}
Replace that with:
if ($classPrefix) {
$blah = explode('/', trim(str_replace($directory, '', $file->getPath()), '/'));
if (count($blah) >= 2 AND $classPrefix == 'Model_') {
if ($blah[0] == 'Generated')
continue;
$className = strtolower($classPrefix.implode('_', $blah).'_'.$className);
} else {
$className = $classPrefix . $className;
}
}
Next open up ‘Doctrine1/lib/Doctrine/Export.php’ and goto line 1092 you’ll see this:
public function exportSchema($directory = null)
{
if ($directory !== null) {
$models = Doctrine_Core::filterInvalidModels(Doctrine_Core::loadModels($directory));
} else {
$models = Doctrine_Core::getLoadedModels();
}
$this->exportClasses($models);
}
And replace it with:
public function exportSchema($directory = null, $classPrefix = null)
{
if ($directory !== null) {
$models = Doctrine_Core::filterInvalidModels(Doctrine_Core::loadModels($directory, null, $classPrefix));
} else {
$models = Doctrine_Core::getLoadedModels();
}
$this->exportClasses($models);
}
Finally if you edit ‘Doctrine1/lib/Doctrine/Import/Builder.php’ and goto line 349 you’ll see this:
if (isset($definition['tableName']) && !empty($definition['tableName'])) {
$ret[$i] = " ".'$this->setTableName(\''. $definition['tableName'].'\');';
$i++;
}
You need to wrap the $definition['tableName'] in a str_replace like:
if (isset($definition['tableName']) && !empty($definition['tableName'])) {
$ret[$i] = " ".'$this->setTableName(\''. str_replace('__', '_', $definition['tableName']).'\');';
$i++;
}
Now you should be able pop schema.yml and data.yml files into your model directories for all active modules and your main application and navigating to ‘http://www.example.com/doctrine/generate’ should with any luck generate all of your models and populate any data you need.
Thank you. It’s very simple & useful descripton. Incredible but to be in good working.
Have a nice day