/** * Generates foreign keys for the plugin table based on the owner table. * These columns are automatically added to the generated model so we can * create foreign keys back to the table object that owns the plugin. * * @param Doctrine_Table $table the table object that owns the plugin * @return array an array of foreign key definitions */ public function buildForeignKeys(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 = $column; $def['primary'] = true; $fk[$col] = $def; } return $fk; }
public function __construct($name, Doctrine_Table $table) { $this->name = $name; $this->table = $table; $this->definition = $table->getDefinitionOf($name); }
/** * Order and Group By are not possible on columns from type text. * This method fix this issue by wrap the given term (column) into a CAST directive. * * @see DC-828 * @param Doctrine_Table $table * @param string $field * @param string $term The term which will changed if it's necessary, depending to the field type. * @return string */ public function modifyOrderByColumn(Doctrine_Table $table, $field, $term) { $def = $table->getDefinitionOf($field); if ($def['type'] == 'string' && $def['length'] === NULL) { $term = 'CAST(' . $term . ' AS varchar(8000))'; } return $term; }
/** * Completes the given definition * * @param array $def definition array to be completed * @return array completed definition array */ public function completeDefinition($def) { $conn = $this->_table->getConnection(); $def['table'] = $conn->getTable($def['class']); $foreignClasses = array_merge($def['table']->getOption('parents'), array($def['class'])); $localClasses = array_merge($this->_table->getOption('parents'), array($this->_table->getComponentName())); if (isset($def['local'])) { if (!isset($def['foreign'])) { // local key is set, but foreign key is not // try to guess the foreign key if ($def['local'] === $this->_table->getIdentifier()) { $def['foreign'] = $this->guessColumns($localClasses, $def['table']); } else { // the foreign field is likely to be the // identifier of the foreign class $def['foreign'] = $def['table']->getIdentifier(); $def['localKey'] = true; } } else { if ($def['local'] !== $this->_table->getIdentifier()) { $def['localKey'] = true; } } } else { if (isset($def['foreign'])) { // local key not set, but foreign key is set // try to guess the local key if ($def['foreign'] === $def['table']->getIdentifier()) { $def['localKey'] = true; try { $def['local'] = $this->guessColumns($foreignClasses, $this->_table); } catch (Doctrine_Relation_Exception $e) { $def['local'] = $this->_table->getIdentifier(); } } else { $def['local'] = $this->_table->getIdentifier(); } } else { // neither local or foreign key is being set // try to guess both keys $conn = $this->_table->getConnection(); // the following loops are needed for covering inheritance foreach ($localClasses as $class) { $table = $conn->getTable($class); $column = strtolower($table->getComponentName()) . '_' . $table->getIdentifier(); foreach ($foreignClasses as $class2) { $table2 = $conn->getTable($class2); if ($table2->hasColumn($column)) { $def['foreign'] = $column; $def['local'] = $table->getIdentifier(); return $def; } } } foreach ($foreignClasses as $class) { $table = $conn->getTable($class); $column = strtolower($table->getComponentName()) . '_' . $table->getIdentifier(); foreach ($localClasses as $class2) { $table2 = $conn->getTable($class2); if ($table2->hasColumn($column)) { $def['foreign'] = $table->getIdentifier(); $def['local'] = $column; $def['localKey'] = true; return $def; } } } // auto-add columns and auto-build relation $columns = array(); foreach ((array) $this->_table->getIdentifier() as $id) { $column = strtolower($table->getComponentName()) . '_' . $id; $col = $this->_table->getDefinitionOf($id); $type = $col['type']; $length = $col['length']; unset($col['type']); unset($col['length']); unset($col['autoincrement']); unset($col['sequence']); unset($col['primary']); $def['table']->setColumn($column, $type, $length, $col); $columns[] = $column; } if (count($columns) > 1) { $def['foreign'] = $columns; } else { $def['foreign'] = $columns[0]; } $def['local'] = $this->_table->getIdentifier(); } } return $def; }