From 2aa986ab85e24b2399e3f9629a77dddb433da5ef Mon Sep 17 00:00:00 2001 From: Djalim Simaila Date: Thu, 22 Feb 2024 09:00:22 +0000 Subject: [PATCH] added userfavorites --- migrations/Version20240222085955.php | 35 ++++++++++++++++++++++++++++ src/Entity/User.php | 34 +++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 migrations/Version20240222085955.php diff --git a/migrations/Version20240222085955.php b/migrations/Version20240222085955.php new file mode 100644 index 0000000..8beaa2e --- /dev/null +++ b/migrations/Version20240222085955.php @@ -0,0 +1,35 @@ +addSql('CREATE TABLE user_album (user_id INT NOT NULL, album_id INT NOT NULL, INDEX IDX_DB5A951BA76ED395 (user_id), INDEX IDX_DB5A951B1137ABCF (album_id), PRIMARY KEY(user_id, album_id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('ALTER TABLE user_album ADD CONSTRAINT FK_DB5A951BA76ED395 FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE'); + $this->addSql('ALTER TABLE user_album ADD CONSTRAINT FK_DB5A951B1137ABCF FOREIGN KEY (album_id) REFERENCES album (id) ON DELETE CASCADE'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE user_album DROP FOREIGN KEY FK_DB5A951BA76ED395'); + $this->addSql('ALTER TABLE user_album DROP FOREIGN KEY FK_DB5A951B1137ABCF'); + $this->addSql('DROP TABLE user_album'); + } +} diff --git a/src/Entity/User.php b/src/Entity/User.php index 52342f1..4220878 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -3,6 +3,8 @@ namespace App\Entity; use App\Repository\UserRepository; +use Doctrine\Common\Collections\ArrayCollection; +use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity; use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface; @@ -29,6 +31,14 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface #[ORM\Column] private ?string $password = null; + #[ORM\ManyToMany(targetEntity: Album::class)] + private Collection $liked; + + public function __construct() + { + $this->liked = new ArrayCollection(); + } + public function getId(): ?int { return $this->id; @@ -98,4 +108,28 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface // If you store any temporary, sensitive data on the user, clear it here // $this->plainPassword = null; } + + /** + * @return Collection + */ + public function getLiked(): Collection + { + return $this->liked; + } + + public function addLiked(Album $liked): static + { + if (!$this->liked->contains($liked)) { + $this->liked->add($liked); + } + + return $this; + } + + public function removeLiked(Album $liked): static + { + $this->liked->removeElement($liked); + + return $this; + } }