Simplify entity relationship and enforce foreign key constraints

Removed the resetStoredObject() method call from StoredObject, adjusted the storedObjectId column in the stored_object_version table to be NOT NULL, and moved the association setup of the StoredObject entity directly into the constructor in StoredObjectVersion. This ensures that every StoredObjectVersion has a valid StoredObject and maintains database integrity.
This commit is contained in:
Julien Fastré 2024-09-04 14:28:13 +02:00
parent 667e144681
commit 615629d1b4
Signed by: julienfastre
GPG Key ID: BDE2190974723FCB
3 changed files with 7 additions and 19 deletions

View File

@ -346,7 +346,6 @@ class StoredObject implements Document, TrackCreationInterface
throw new \UnexpectedValueException('This stored object does not contains this version');
}
$this->versions->removeElement($storedObjectVersion);
$storedObjectVersion->resetStoredObject();
}
/**

View File

@ -33,13 +33,6 @@ class StoredObjectVersion implements TrackCreationInterface
#[ORM\Column(type: \Doctrine\DBAL\Types\Types::INTEGER)]
private ?int $id = null;
/**
* The stored object associated with this version.
*/
#[ORM\ManyToOne(targetEntity: StoredObject::class, inversedBy: 'versions')]
#[ORM\JoinColumn(name: 'stored_object_id', nullable: true)]
private ?StoredObject $storedObject;
/**
* filename of the version in the stored object.
*/
@ -47,7 +40,12 @@ class StoredObjectVersion implements TrackCreationInterface
private string $filename = '';
public function __construct(
StoredObject $storedObject,
/**
* The stored object associated with this version.
*/
#[ORM\ManyToOne(targetEntity: StoredObject::class, inversedBy: 'versions')]
#[ORM\JoinColumn(name: 'stored_object_id', nullable: false)]
private StoredObject $storedObject,
/**
* The incremental version.
@ -78,7 +76,6 @@ class StoredObjectVersion implements TrackCreationInterface
private string $type = '',
?string $filename = null,
) {
$this->storedObject = $storedObject;
$this->filename = $filename ?? self::generateFilename($this);
}
@ -127,12 +124,4 @@ class StoredObjectVersion implements TrackCreationInterface
{
return $this->version;
}
/**
* @internal to be used by StoredObject::removeVersion
*/
public function resetStoredObject(): void
{
$this->storedObject = null;
}
}

View File

@ -28,7 +28,7 @@ final class Version20240709102730 extends AbstractMigration
<<<'SQL'
CREATE TABLE chill_doc.stored_object_version (
id INT NOT NULL,
stored_object_id INT DEFAULT NULL,
stored_object_id INT NOT NULL,
version INT DEFAULT 0 NOT NULL,
filename TEXT NOT NULL,
iv JSON NOT NULL,