From a9e5a35917a154b5d112c24b9a3d5c45f8d5aafa Mon Sep 17 00:00:00 2001 From: zYne Date: Sat, 13 Oct 2007 16:27:47 +0000 Subject: [PATCH] plugin refactoring continues --- lib/Doctrine/Plugin.php | 60 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/lib/Doctrine/Plugin.php b/lib/Doctrine/Plugin.php index 787b5d619..9a6a7a1c9 100644 --- a/lib/Doctrine/Plugin.php +++ b/lib/Doctrine/Plugin.php @@ -98,7 +98,65 @@ class Doctrine_Plugin { return $this->_options; } - + /** + * generates foreign keys for the plugin table based on the owner table + * + * the foreign keys generated by this method can be used for + * setting the relations between the owner and the plugin classes + * + * @param Doctrine_Table $table the table object that owns the plugin + * @return array an array of foreign key definitions + */ + public function generateForeignKeys(Doctrine_Table $table) + { + $fk = array(); + + foreach ((array) $table->getIdentifier() as $column) { + $def = $table->getDefinitionOf($column); + + unset($def['autoincrement']); + unset($def['sequence']); + unset($def['primary']); + + $col = strtolower(Doctrine::tableize($table->getComponentName()) . '_' . $column); + + $def['primary'] = true; + $fk[$col] = $def; + } + return $fk; + } + /** + * generates a relation array to given table + * + * this method can be used for generating the relation from the plugin + * table to the owner table + * + * @param Doctrine_Table $table the table object to construct the relation to + * @param array $foreignKeys an array of foreign keys + * @return array the generated relation array + */ + public function generateRelation(Doctrine_Table $table, array $foreignKeys) + { + $local = (count($foreignKeys) > 1) ? array_keys($foreignKeys) : key($foreignKeys); + + $relation = array($table->getComponentName() => + array('local' => $local, + 'foreign' => $table->getIdentifier(), + 'onDelete' => 'CASCADE', + 'onUpdate' => 'CASCADE')); + + return $relation; + } + /** + * generates the class definition for plugin class + * + * @param array $options plugin class options, keys representing the option names + * and values as option values + * @param array $columns the plugin class columns, keys representing the column names + * and values as column definitions + * @param array $relations the bound relations of the plugin class + * @return void + */ public function generateClass($options, $columns, $relations) { $builder = new Doctrine_Import_Builder();