Skip to content

Commit

Permalink
Fix deleting related entities
Browse files Browse the repository at this point in the history
  • Loading branch information
D3strukt0r committed May 2, 2024
1 parent c3908bf commit 6417957
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"recommendations": ["svelte.svelte-vscode"]
"recommendations": []
}
1 change: 1 addition & 0 deletions Vagrantfile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Vagrant.configure('2') do |config|
if File.exist?('.vagrant.config.yml.dist')
settingsDist = YAML.load(File.read('.vagrant.config.yml.dist'))
end
# TODO: Handle empty file "in `merge': no implicit conversion of nil into Hash (TypeError)"
if File.exist?('.vagrant.config.yml')
settingsCustom = YAML.load(File.read('.vagrant.config.yml'))
end
Expand Down
41 changes: 41 additions & 0 deletions api/migrations/Version20240502173703.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* Auto-generated Migration: Please modify to your needs!
*/
final class Version20240502173703 extends AbstractMigration
{
public function getDescription(): string
{
return 'Fix deletion of related entities on cascade';
}

public function up(Schema $schema): void
{
// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE card DROP FOREIGN KEY FK_161498D3BC3F045D');
$this->addSql('ALTER TABLE card ADD CONSTRAINT FK_161498D3BC3F045D FOREIGN KEY (user_login_id) REFERENCES user (id) ON DELETE SET NULL');
$this->addSql('ALTER TABLE invitee DROP FOREIGN KEY FK_F7AADF3D759A837E');
$this->addSql('ALTER TABLE invitee DROP FOREIGN KEY FK_F7AADF3D4ACC9A20');
$this->addSql('ALTER TABLE invitee ADD CONSTRAINT FK_F7AADF3D759A837E FOREIGN KEY (table_to_sit_id) REFERENCES `table` (id) ON DELETE SET NULL');
$this->addSql('ALTER TABLE invitee ADD CONSTRAINT FK_F7AADF3D4ACC9A20 FOREIGN KEY (card_id) REFERENCES card (id) ON DELETE SET NULL');
}

public function down(Schema $schema): void
{
// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE invitee DROP FOREIGN KEY FK_F7AADF3D759A837E');
$this->addSql('ALTER TABLE invitee DROP FOREIGN KEY FK_F7AADF3D4ACC9A20');
$this->addSql('ALTER TABLE invitee ADD CONSTRAINT FK_F7AADF3D759A837E FOREIGN KEY (table_to_sit_id) REFERENCES `table` (id)');
$this->addSql('ALTER TABLE invitee ADD CONSTRAINT FK_F7AADF3D4ACC9A20 FOREIGN KEY (card_id) REFERENCES card (id)');
$this->addSql('ALTER TABLE card DROP FOREIGN KEY FK_161498D3BC3F045D');
$this->addSql('ALTER TABLE card ADD CONSTRAINT FK_161498D3BC3F045D FOREIGN KEY (user_login_id) REFERENCES user (id)');
}
}
4 changes: 2 additions & 2 deletions api/src/Entity/Card.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class Card
#[ORM\OneToMany(mappedBy: 'card', targetEntity: Invitee::class)]
private Collection $invitees;

#[ORM\OneToOne(inversedBy: 'card', cascade: ['persist', 'remove'])]
#[ORM\JoinColumn(nullable: true)]
#[ORM\OneToOne(inversedBy: 'card', cascade: ['persist'])]
#[ORM\JoinColumn(nullable: true, onDelete: 'SET NULL')]
private ?User $userLogin = null;

public function __construct()
Expand Down
3 changes: 2 additions & 1 deletion api/src/Entity/Invitee.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@ class Invitee
private ?string $allergies = null;

#[ORM\ManyToOne(inversedBy: 'invitees')]
#[ORM\JoinColumn(name: 'table_to_sit_id')]
#[ORM\JoinColumn(name: 'table_to_sit_id', onDelete: 'SET NULL')]
private ?Table $table = null;

#[ORM\ManyToOne(inversedBy: 'invitees')]
#[ORM\JoinColumn(onDelete: 'SET NULL')]
private ?Card $card = null;

public function __construct(string $firstname, string $lastname)
Expand Down
2 changes: 1 addition & 1 deletion api/src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
#[ORM\Column(nullable: false)]
private string $password;

#[ORM\OneToOne(mappedBy: 'userLogin', cascade: ['persist', 'remove'])]
#[ORM\OneToOne(mappedBy: 'userLogin', cascade: ['persist'])]
private ?Card $card = null;

public function __construct(string $username, UserPasswordHasherInterface $passwordHasher, string $password)
Expand Down

0 comments on commit 6417957

Please sign in to comment.