diff --git a/CHANGELOG.md b/CHANGELOG.md index f909269f8..0d2deb74d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -56,7 +56,8 @@ - Enh #919: Replace `name()` with immutable `withName()` method in `ColumnInterface` interface (@Tigrov) - Enh #921: Move `DataType` class to `Yiisoft\Db\Constant` namespace (@Tigrov) - Enh #926: Refactor `DbArrayHelper` (@Tigrov) -- Enh #920: Move index constants to the appropriate DBMS driver's `IndexType` and `IndexMethod` classes (@Tigrov) +- Enh #920: Move index constants to the appropriate DBMS driver's `IndexType` and `IndexMethod` classes (@Tigrov) +- New #928: Add `ReferentialAction` class with constants of possible values of referential actions (@Tigrov) ## 1.3.0 March 21, 2024 diff --git a/UPGRADE.md b/UPGRADE.md index de51062aa..5a616cb16 100644 --- a/UPGRADE.md +++ b/UPGRADE.md @@ -96,7 +96,8 @@ and the following changes were made: - `Yiisoft\Db\Constant\GettypeResult` with `gettype()` function results constants; - `Yiisoft\Db\Constant\ColumnType` with abstract column types constants; - `Yiisoft\Db\Constant\PseudoType` with column pseudo-types constants; -- `Yiisoft\Db\Constant\IndexType` with table index types. +- `Yiisoft\Db\Constant\IndexType` with table index types; +- `Yiisoft\Db\Constant\ReferentialAction` with possible values of referential actions. ### New classes for table columns diff --git a/docs/guide/en/command/ddl.md b/docs/guide/en/command/ddl.md index 96519aa4c..d740e3bd9 100644 --- a/docs/guide/en/command/ddl.md +++ b/docs/guide/en/command/ddl.md @@ -181,6 +181,7 @@ method: ```php use Yiisoft\Db\Connection\ConnectionInterface; +use Yiisoft\Db\Constant\ReferentialAction; /** @var ConnectionInterface $db */ $db->createCommand()->addForeignKey( @@ -189,8 +190,8 @@ $db->createCommand()->addForeignKey( 'profile_id', '{{%profile}}', 'id', - 'CASCADE', - 'CASCADE' + ReferentialAction::CASCADE, + ReferentialAction::CASCADE )->execute(); ``` diff --git a/docs/guide/pt-BR/command/ddl.md b/docs/guide/pt-BR/command/ddl.md index 82d22cf7f..ebaa68498 100644 --- a/docs/guide/pt-BR/command/ddl.md +++ b/docs/guide/pt-BR/command/ddl.md @@ -177,6 +177,7 @@ Para adicionar uma chave estrangeira a uma tabela existente, vocĂȘ pode usar o m ```php use Yiisoft\Db\Connection\ConnectionInterface; +use Yiisoft\Db\Constant\ReferentialAction; /** @var ConnectionInterface $db */ $db->createCommand()->addForeignKey( @@ -185,8 +186,8 @@ $db->createCommand()->addForeignKey( 'profile_id', '{{%profile}}', 'id', - 'CASCADE', - 'CASCADE' + ReferentialAction::CASCADE, + ReferentialAction::CASCADE )->execute(); ``` diff --git a/src/Command/CommandInterface.php b/src/Command/CommandInterface.php index f88b469bf..8801050cd 100644 --- a/src/Command/CommandInterface.php +++ b/src/Command/CommandInterface.php @@ -12,6 +12,7 @@ use Yiisoft\Db\Constant\DataType; use Yiisoft\Db\Constant\IndexType; use Yiisoft\Db\Constant\PseudoType; +use Yiisoft\Db\Constant\ReferentialAction; use Yiisoft\Db\Exception\Exception; use Yiisoft\Db\Exception\InvalidArgumentException; use Yiisoft\Db\Exception\InvalidCallException; @@ -99,8 +100,7 @@ public function addDefaultValue(string $table, string $name, string $column, mix /** * Creates an SQL command for adding a foreign key constraint to an existing table. - * - * The method will quote the table and column names. + * The method will quote the `name`, `table`, `referenceTable` parameters before using them in the generated SQL. * * @param string $table The name of the table to add foreign key constraint to. * @param string $name The name of the foreign key constraint. @@ -109,16 +109,14 @@ public function addDefaultValue(string $table, string $name, string $column, mix * @param string $referenceTable The name of the table that the foreign key references to. * @param array|string $referenceColumns The name of the column that the foreign key references to. If there are * many columns, separate them with commas. - * @param string|null $delete The `ON DELETE` option. Most DBMS support these options: `RESTRICT`, `CASCADE`, `NO ACTION`, - * `SET DEFAULT`, `SET NULL`. - * @param string|null $update The `ON UPDATE` option. Most DBMS support these options: `RESTRICT`, `CASCADE`, `NO ACTION`, - * `SET DEFAULT`, `SET NULL`. + * @param string|null $delete The `ON DELETE` option. See {@see ReferentialAction} class for possible values. + * @param string|null $update The `ON UPDATE` option. See {@see ReferentialAction} class for possible values. * * @throws Exception * @throws InvalidArgumentException * - * Note: The method will quote the `name`, `table`, `referenceTable` parameters before using them in the generated - * SQL. + * @psalm-param ReferentialAction::*|null $delete + * @psalm-param ReferentialAction::*|null $update */ public function addForeignKey( string $table, @@ -126,8 +124,8 @@ public function addForeignKey( array|string $columns, string $referenceTable, array|string $referenceColumns, - string $delete = null, - string $update = null + string|null $delete = null, + string|null $update = null ): static; /** diff --git a/src/Constant/ReferentialAction.php b/src/Constant/ReferentialAction.php new file mode 100644 index 000000000..b05c913c5 --- /dev/null +++ b/src/Constant/ReferentialAction.php @@ -0,0 +1,46 @@ +getConnection(); - $command = $db->createCommand(); - $sql = $command->addForeignKey($tableName, $name, $column1, $tableName, $column2, $delete, $update)->getSql(); + + $name = '{{fk_constraint}}'; + $tableName = '{{fk_table}}'; + $referenceTable = '{{fk_referenced_table}}'; + + $sql = $command->addForeignKey($tableName, $name, $columns, $referenceTable, $referenceColumns, $delete, $update)->getSql(); $this->assertSame($expected, $sql); } diff --git a/tests/Provider/CommandProvider.php b/tests/Provider/CommandProvider.php index 7f559e01f..bb3bf6d8d 100644 --- a/tests/Provider/CommandProvider.php +++ b/tests/Provider/CommandProvider.php @@ -11,6 +11,7 @@ use Yiisoft\Db\Command\Param; use Yiisoft\Db\Constant\ColumnType; use Yiisoft\Db\Constant\IndexType; +use Yiisoft\Db\Constant\ReferentialAction; use Yiisoft\Db\Expression\Expression; use Yiisoft\Db\Query\Query; use Yiisoft\Db\Schema\Column\ColumnBuilder; @@ -38,113 +39,121 @@ public static function addForeignKeySql(): array { return [ [ - '{{test_fk_constraint_1}}', - '{{test_fk}}', 'int1', 'int3', null, null, DbHelper::replaceQuotes( <<foreignColumnNames(['id']); $reference->foreignTableName('ref_table'); - $reference->onDelete('CASCADE'); - $reference->onUpdate('CASCADE'); + $reference->onDelete(ReferentialAction::CASCADE); + $reference->onUpdate(ReferentialAction::CASCADE); $referenceWithSchema = clone $reference; $referenceWithSchema->foreignSchemaName('ref_schema'); diff --git a/tests/Provider/SchemaProvider.php b/tests/Provider/SchemaProvider.php index fe840547d..970826173 100644 --- a/tests/Provider/SchemaProvider.php +++ b/tests/Provider/SchemaProvider.php @@ -6,6 +6,7 @@ use PDO; use Yiisoft\Db\Constant\IndexType; +use Yiisoft\Db\Constant\ReferentialAction; use Yiisoft\Db\Constraint\CheckConstraint; use Yiisoft\Db\Constraint\Constraint; use Yiisoft\Db\Constraint\ForeignKeyConstraint; @@ -105,8 +106,8 @@ public static function constraints(): array ->columnNames(['C_fk_id_1', 'C_fk_id_2']) ->foreignTableName('T_constraints_2') ->foreignColumnNames(['C_id_1', 'C_id_2']) - ->onDelete('CASCADE') - ->onUpdate('CASCADE'), + ->onDelete(ReferentialAction::CASCADE) + ->onUpdate(ReferentialAction::CASCADE), ], ], '3: unique' => ['T_constraints_3', SchemaInterface::UNIQUES, []],