Database/src/Migrations/Diff/TableForeignKey.php

103 lines
1.9 KiB
PHP
Raw Normal View History

2024-11-08 16:33:44 +01:00
<?php
namespace Nest\Database\Migrations\Diff;
/**
* Table foreign key configurator.
*/
class TableForeignKey
{
/**
* Foreign key columns.
* @var string[]
*/
public array $columns = [];
/**
* Referenced table.
* @var string
*/
public string $referencedTable;
/**
* Referenced columns.
* @var string[]
*/
public array $referencedColumns;
/**
* Action on delete.
* @var string
*/
public string $onDelete;
/**
* Action on update.
* @var string
*/
public string $onUpdate;
/**
* Create a new table foreign key.
* @param Table $table Table of the foreign key.
*/
public function __construct(public readonly Table $table)
{
}
/**
* Add foreign key columns.
* @param string ...$columns Foreign key columns.
* @return $this
*/
public function columns(string ...$columns): static
{
array_push($this->columns, ...$columns);
return $this;
}
/**
* Set referenced table and columns..
* @param string $referencedTable Referenced table name.
* @param string[] $referencedColumns Referenced columns in table.
* @return $this
*/
public function references(string $referencedTable, string ...$referencedColumns): static
{
$this->referencedTable = $referencedTable;
$this->referencedColumns = $referencedColumns;
return $this;
}
/**
* Set action on delete.
* @param string $referentialAction Raw action.
* @return $this
*/
public function onDelete(string $referentialAction): static
{
$this->onDelete = $referentialAction;
return $this;
}
/**
* Set action on update.
* @param string $referentialAction Raw action.
* @return $this
*/
public function onUpdate(string $referentialAction): static
{
$this->onUpdate = $referentialAction;
return $this;
}
/**
* Create the defined foreign key.
* @return void
*/
public function create(): void
{
$this->table->getDatabase()->getQueriesAdapter()->createForeignKey($this);
}
}