Replies: 2 comments
-
If this is going to be implemented, this could be another general syntax for convenience, defaulting to what you suggest like this: use Conditionable;
/**
* Indicate that updates and deletes should cascade.
*
* @return $this
*/
public function cascade(bool $onUpdate = true, bool $onDelete = true)
{
return $this
->when($onUpdate, fn() => $this->onUpdate('cascade'))
->when($onDelete, fn() => $this->onDelete('cascade'));
}
/**
* Indicate that updates and deletes should be restricted.
*
* @return $this
*/
public function restrict(bool $onUpdate = true, bool $onDelete = true)
{
return $this
->when($onUpdate, fn() => $this->onUpdate('restrict'))
->when($onDelete, fn() => $this->onDelete('restrict'));
} So we could do $table->foreignKeyFor('foreign_key_column')->cascade(); // cascade both
$table->foreignKeyFor('foreign_key_column')->cascade(onDelete: false); // cascade only onUpdate
$table->foreignKeyFor('foreign_key_column')->cascade(onUpdate: false); // cascade only onDelete Or maybe in a less negated way: use Conditionable;
/**
* Indicate that updates and deletes should cascade.
*
* @param string[] $methods
*
* @return $this
*/
public function cascade(array $methods = ['update', 'delete'])
{
return $this
->when(in_array('update', $methods), fn() => $this->onUpdate('cascade'))
->when(in_array('delete', $methods), fn() => $this->onDelete('cascade'));
}
/**
* Indicate that updates and deletes should be restricted.
*
* @param string[] $methods
*
* @return $this
*/
public function restrict(array $methods = ['update', 'delete'])
{
return $this
->when(in_array('update', $methods), fn() => $this->onUpdate('restrict'))
->when(in_array('delete', $methods), fn() => $this->onDelete('restrict'));
}
/**
* @throws LengthException when array size is higher than 2
* @throws UnexpectedValueException when array contains a value other than 'update' or 'delete'
*/
private function checkMethods(array $methods); |
Beta Was this translation helpful? Give feedback.
-
I found the idea of combining the cascading methods for 'onUpdate' and 'onDelete' quite interesting. However, I found it unnecessary to include the 'onUpdate' and 'onDelete' parameters within the methods. This way, developers can choose to use cascadeOnUpdate or restrictOnUpdate() specifically, rather than adding two boolean parameters to onUpdate or onDelete. // I believe it would not be suitable for code readability.
$table->foreignKeyFor('foreign_key_column')->cascade(onDelete: false)->restrict(onUpdate: false);
$table->foreignKeyFor('foreign_key_column')->cascade(false)->restrict(true, false);
$table->foreignKeyFor('foreign_key_column')->cascade(['update'])->restrict(['delete']); A better approach would be, in my opinionAs the Laravel framework has the methods cascadeOnUpdate, cascadeOnDelete, restrictOnUpdate, restrictOnDelete, ... According to the documentation and the table below.
you can rename the cascadeOnUpdateOnDelete() and restrictOnUpdateOnDelete() method and implementation would also be easier you are going to implement in file framework/src/Illuminate/Database/Schema/ForeignKeyDefinition.php /**
* Indicate that updates and deletes should cascade.
*
* @return $this
*/
public function cascadeOnUpdateOnDelete()
{
return $this->cascadeOnUpdate()->cascadeOnDelete();
} /**
* Indicate that updates and deletes should be restricted.
*
* @return $this
*/
public function restrictOnUpdateOnDelete()
{
return $this->restrictOnUpdate()->restrictOnDelete();
} There is no need to include condition inside the method because of two parameters 'onUpdate' and 'onDelete'. This way, I believe that implementation and maintenance are easier without the need to include a condition. Regarding the name of the restrictOnUpdateOnDelete and cascadeOnUpdateOnDelete function, it is objective and clear what it proposes. $table->foreignKeyFor('foreign_key_column')->cascadeOnUpdateOnDelete(); in the documentation it would be:
|
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hello, community, what do you think about appending "cascade()" and "restrict()" methods, which combine "cascadeOnUpdate() + cascadeOnDelete()" and "restrictOnUpdate() + cascadeOnDelete()" accordingly, for a more compact code?
Beta Was this translation helpful? Give feedback.
All reactions