/** * Returns all un-ignored relations * @return array */ protected function _getRelations() { $relations = array(); foreach ($this->_table->getRelations() as $name => $definition) { if (in_array($definition->getLocal(), $this->_ignoreColumns) || $this->_generateManyFields == false && $definition->getType() == Doctrine_Relation::MANY) { continue; } $relations[$name] = $definition; } return $relations; }
/** * Returns true if this column is a foreign key and false if it is not * * @return boolean $isForeignKey */ public function isForeignKey() { if (isset($this->foreignClassName)) { return true; } if ($this->isPrimaryKey()) { return false; } foreach ($this->table->getRelations() as $relation) { if (strtolower($relation['local']) == strtolower($this->name)) { $this->foreignClassName = $relation['class']; return true; } } return false; }
/** * Return relations as an array * * Array must contain 'type' for relation type, 'id' for the name * of the PK column of the related table, 'model' for the related class * name, 'notnull' for nullability. 'local' for the name of the local column * Key must be the alias of the relation column * * @return array */ public function getManyRelations() { $rels = $this->_table->getRelations(); $relations = array(); foreach ($rels as $rel) { $relation = array(); if ($rel->getType() == Doctrine_Relation::MANY && isset($rel['refTable'])) { $relation['id'] = $rel->getTable()->getIdentifier(); $relation['model'] = $rel->getClass(); $relation['local'] = $rel->getLocal(); $definition = $this->_table->getColumnDefinition($rel->getLocal()); $relation['notnull'] = isset($definition['notnull']) ? $definition['notnull'] : false; $relations[$rel->getAlias()] = $relation; } } return $relations; }
/** * Returns true if this column is a foreign key and false if it is not * * @return boolean $isForeignKey */ public function isForeignKey() { if (isset($this->foreignClassName)) { return true; } if ($this->isPrimaryKey()) { return false; } foreach ($this->table->getRelations() as $relation) { $local = (array) $relation['local']; $local = array_map('strtolower', $local); if (in_array(strtolower($this->name), $local)) { $this->foreignClassName = $relation['class']; return true; } } return false; }
/** * buildIntegrityRelations * * @param Doctrine_Table $table * @param mixed $aliases * @param mixed $fields * @param mixed $indexes * @param mixed $components * @return void */ public function buildIntegrityRelations(Doctrine_Table $table, &$aliases, &$fields, &$indexes, &$components) { $deleteActions = Doctrine_Manager::getInstance() ->getDeleteActions($table->getComponentName()); foreach ($table->getRelations() as $relation) { $componentName = $relation->getTable()->getComponentName(); if (in_array($componentName, $components)) { continue; } $components[] = $componentName; $alias = strtolower(substr($relation->getAlias(), 0, 1)); if ( ! isset($indexes[$alias])) { $indexes[$alias] = 1; } if (isset($deleteActions[$componentName])) { if (isset($aliases[$alias])) { $alias = $alias . ++$indexes[$alias]; } $aliases[$alias] = $relation->getAlias(); if ($deleteActions[$componentName] === 'SET NULL') { if ($relation instanceof Doctrine_Relation_ForeignKey) { foreach ((array) $relation->getForeign() as $foreign) { $fields .= ', ' . $alias . '.' . $foreign; } } elseif ($relation instanceof Doctrine_Relation_LocalKey) { foreach ((array) $relation->getLocal() as $foreign) { $fields .= ', ' . $alias . '.' . $foreign; } } } foreach ((array) $relation->getTable()->getIdentifier() as $id) { $fields .= ', ' . $alias . '.' . $id; } if ($deleteActions[$componentName] === 'CASCADE') { $this->buildIntegrityRelations($relation->getTable(), $aliases, $fields, $indexes, $components); } } } }
/** * Provides a array of columns * * @param Doctrine_Table $table The current Doctrine_Table * * @return array */ private function listColumns(Doctrine_Table $table) { $ret = array(); foreach ($table->getColumns() as $name => $column) { if (empty($column['primary'])) { $added = true; foreach ($table->getRelations() as $relation) { if ($relation instanceof Doctrine_Relation_LocalKey && $relation->getLocal() === $name) { $added = false; break; } } if ($added) { $ret[] = sprintf("%s (%s)", $name, $column['type']); } } } return $ret; }