ModelCode::generateRelations PHP Method

generateRelations() protected method

protected generateRelations ( )
    protected function generateRelations()
    {
        $relations = array();
        foreach (Yii::app()->{$this->connectionId}->schema->getTables() as $table) {
            if ($this->tablePrefix != '' && strpos($table->name, $this->tablePrefix) !== 0) {
                continue;
            }
            $tableName = $table->name;
            if ($this->isRelationTable($table)) {
                $pks = $table->primaryKey;
                $fks = $table->foreignKeys;
                $table0 = $fks[$pks[0]][0];
                $table1 = $fks[$pks[1]][0];
                $className0 = $this->generateClassName($table0);
                $className1 = $this->generateClassName($table1);
                $unprefixedTableName = $this->removePrefix($tableName);
                $relationName = $this->generateRelationName($table0, $table1, true);
                $relations[$className0][$relationName] = "array(self::MANY_MANY, '{$className1}', '{$unprefixedTableName}({$pks['0']}, {$pks['1']})')";
                $relationName = $this->generateRelationName($table1, $table0, true);
                $relations[$className1][$relationName] = "array(self::MANY_MANY, '{$className0}', '{$unprefixedTableName}({$pks['1']}, {$pks['0']})')";
            } else {
                $className = $this->generateClassName($tableName);
                foreach ($table->foreignKeys as $fkName => $fkEntry) {
                    // Put table and key name in variables for easier reading
                    $refTable = $fkEntry[0];
                    // Table name that current fk references to
                    $refKey = $fkEntry[1];
                    // Key in that table being referenced
                    $refClassName = $this->generateClassName($refTable);
                    // Add relation for this table
                    $relationName = $this->generateRelationName($tableName, $fkName, false);
                    $relations[$className][$relationName] = "array(self::BELONGS_TO, '{$refClassName}', '{$fkName}')";
                    // Add relation for the referenced table
                    $relationType = $table->primaryKey === $fkName ? 'HAS_ONE' : 'HAS_MANY';
                    $relationName = $this->generateRelationName($refTable, $this->removePrefix($tableName, false), $relationType === 'HAS_MANY');
                    $relations[$refClassName][$relationName] = "array(self::{$relationType}, '{$className}', '{$fkName}')";
                }
            }
        }
        return $relations;
    }

Usage Example

Ejemplo n.º 1
0
 public function generateRelations()
 {
     $relations = parent::generateRelations();
     $className = $this->generateClassName($this->tableName);
     foreach ($this->getObject()->parameters as $parameter) {
         //Формируем relations для свойства "Объект (внешний ключ)"
         if ($parameter->id_parameter_type == DataType::OBJECT) {
             $refObject = DaObject::getById($parameter->add_parameter);
             $refClassName = $this->generateClassName($this->removeTablePrefix($refObject->table_name));
             $relationName = $this->generateRelationName($refObject->table_name, $this->removeFieldNamePrefix($parameter->field_name), false);
             if (!isset($relations[$className][$relationName])) {
                 $relations[$className][$relationName] = "array(self::BELONGS_TO, '{$refClassName}', '{$parameter->field_name}')";
             }
         } elseif ($parameter->id_parameter_type == DataType::FILE) {
             $refClassName = 'File';
             $relationName = $this->generateRelationName('da_files', $this->removeFieldNamePrefix($parameter->field_name), false);
             if ($relationName == $this->removeFieldNamePrefix($parameter->field_name)) {
                 $relationName .= 'File';
             }
             if (!isset($relations[$className][$relationName])) {
                 $relations[$className][$relationName] = "array(self::BELONGS_TO, '{$refClassName}', '{$parameter->field_name}')";
             }
         } elseif ($parameter->id_parameter_type == DataType::REFERENCE) {
             //TODO Подумать нужно ли?
             /*
             $refClassName = 'ReferenceElement';
             $relationName = $this->generateRelationName('da_reference_element', $this->removeFieldNamePrefix($parameter->field_name), false);
             if (!isset($relations[$className][$relationName])) {
               $relations[$className][$relationName]="array(self::BELONGS_TO, '$refClassName', '$parameter->field_name'), 'on' => 'id_reference = $parameter->add_parameter'";
             }
             */
         }
     }
     //формируем relations для подчиненных таблиц
     $parameters = ObjectParameter::model()->findAllByAttributes(array('id_parameter_type' => DataType::OBJECT, 'add_parameter' => $this->getObject()->id_object));
     foreach ($parameters as $parameter) {
         $refObject = DaObject::getById($parameter->id_object);
         $refClassName = $this->generateClassName($this->removeTablePrefix($refObject->table_name));
         $relationName = $this->generateRelationName($refObject->table_name, $this->removeTablePrefix($refObject->table_name), true);
         if (!isset($relations[$className][$relationName])) {
             $relations[$className][$relationName] = "array(self::HAS_MANY, '{$refClassName}', '{$parameter->field_name}')";
         }
     }
     return $relations;
 }