/** * @param ClassMetadataBuilder $metadataBuilder * @param FieldConfigId $fieldId * @param string $targetEntity * @param FieldConfigId|null $targetFieldId * @param string[] $cascade */ protected function buildManyToOneRelation(ClassMetadataBuilder $metadataBuilder, FieldConfigId $fieldId, $targetEntity, FieldConfigId $targetFieldId = null, array $cascade = []) { $builder = $metadataBuilder->createManyToOne($fieldId->getFieldName(), $targetEntity); if ($targetFieldId) { $builder->inversedBy($targetFieldId->getFieldName()); } $builder->addJoinColumn($this->nameGenerator->generateManyToOneRelationColumnName($fieldId->getFieldName()), 'id', true, false, 'SET NULL'); foreach ($cascade as $cascadeType) { $builder->{'cascade' . ucfirst($cascadeType)}(); } $builder->build(); }
protected function renameManyToOneExtendField(Schema $schema, QueryBag $queries, Table $table, $associationName) { $oldColumnName = 'field_' . $associationName . '_id'; if ($table->hasColumn($oldColumnName)) { $newColumnName = $this->nameGenerator->generateManyToOneRelationColumnName($associationName); $this->renameExtension->renameColumn($schema, $queries, $table, $oldColumnName, $newColumnName); } }
/** * Adds many-to-one relation * * @param Schema $schema * @param Table|string $table A Table object or table name * @param string $associationName A relation name * @param Table|string $targetTable A Table object or table name * @param string $targetColumnName A column name is used to show related entity * @param array $options * @param string $fieldType The field type. By default the field type is manyToOne, * but you can specify another type if it is based on manyToOne. * In this case this type should be registered * in entity_extend.yml under underlying_types section */ public function addManyToOneRelation(Schema $schema, $table, $associationName, $targetTable, $targetColumnName, array $options = [], $fieldType = RelationType::MANY_TO_ONE) { $this->ensureExtendFieldSet($options); $selfTableName = $this->getTableName($table); $selfTable = $this->getTable($table, $schema); $selfColumnName = $this->nameGenerator->generateManyToOneRelationColumnName($associationName); $targetTableName = $this->getTableName($targetTable); $targetTable = $this->getTable($targetTable, $schema); $targetPrimaryKeyColumnName = $this->getPrimaryKeyColumnName($targetTable); $targetPrimaryKeyColumn = $targetTable->getColumn($targetPrimaryKeyColumnName); $this->checkColumnsExist($targetTable, [$targetColumnName]); $this->addRelationColumn($selfTable, $selfColumnName, $targetPrimaryKeyColumn, ['notnull' => false]); $selfTable->addIndex([$selfColumnName]); $selfTable->addForeignKeyConstraint($targetTable, [$selfColumnName], [$targetPrimaryKeyColumnName], ['onDelete' => 'SET NULL']); $options[ExtendOptionsManager::TARGET_OPTION] = ['table_name' => $targetTableName, 'column' => $targetColumnName]; $options[ExtendOptionsManager::TYPE_OPTION] = $fieldType; $this->extendOptionsManager->setColumnOptions($selfTableName, $associationName, $options); }
/** * Gets an association column name for note relation * * @param string $targetTableName Target entity table name. * * @return string */ public function getAssociationColumnName($targetTableName) { $associationName = ExtendHelper::buildAssociationName($this->extendExtension->getEntityClassByTableName($targetTableName)); return $this->nameGenerator->generateManyToOneRelationColumnName($associationName); }