Final Class Yiisoft\Db\Migration\Service\Generate\ForeignKeyFactory
| Inheritance | Yiisoft\ |
|---|
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\ |
|
| create() | Yiisoft\ |
Method Details
| public mixed __construct ( \ | ||
| $db | \ |
|
| $io | ?\ |
|
| $useTablePrefix | boolean | |
public function __construct(
private readonly ConnectionInterface $db,
private readonly ?SymfonyStyle $io,
private readonly bool $useTablePrefix,
) {}
| public Yiisoft\ | ||
| $table | string | |
| $column | string | |
| $relatedTable | string | |
| $relatedColumn | ?string | |
public function create(
string $table,
string $column,
string $relatedTable,
?string $relatedColumn,
): ForeignKey {
/**
* We're trying to get it from the table schema.
*
* {@see https://github.com/yiisoft/yii2/issues/12748}
*/
if ($relatedColumn === null) {
$relatedColumn = 'id';
$tablePrimaryKeys = $this->db->getSchema()->getTablePrimaryKey($relatedTable);
if ($tablePrimaryKeys !== null) {
$primaryKeys = $tablePrimaryKeys->columnNames;
match (count($primaryKeys)) {
1 => $relatedColumn = $primaryKeys[0],
default => $this->io?->writeln(
"<fg=yellow> Related table for field \"$column\" exists, but primary key is composite. Default name \"id\" will be used for related field</>\n" ,
),
};
} else {
$this->io?->writeln(
"<fg=yellow> Related table for field \"$column\" exists, but does not have a primary key. Default name \"id\" will be used for related field.</>\n" ,
);
}
}
return new ForeignKey(
"idx-$table-$column",
"fk-$table-$column",
$column,
$this->useTablePrefix ? '{{%' . $relatedTable . '}}' : $relatedTable,
$relatedColumn,
);
}
User Contributed Notes
Leave a comment
Join the conversation to share a note.