Skip to content

Commit

Permalink
feat(user): add project relation
Browse files Browse the repository at this point in the history
* update user entity

* update project entity

* make migration

see: #20, #30
  • Loading branch information
n3wborn committed Jan 13, 2024
1 parent 72d0933 commit e0bdb7c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 12 deletions.
32 changes: 32 additions & 0 deletions migrations/Version20231101095000.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

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

final class Version20231101095000 extends AbstractMigration
{
public function getDescription(): string
{
return 'Add User <-> Project relation';
}

public function up(Schema $schema): void
{
$this->addSql('ALTER TABLE project ADD user_project_id UUID DEFAULT NULL');
$this->addSql('COMMENT ON COLUMN project.user_project_id IS \'(DC2Type:uuid)\'');
$this->addSql('ALTER TABLE project ADD CONSTRAINT FK_2FB3D0EEB10AD970 FOREIGN KEY (user_project_id) REFERENCES "user" (id) NOT DEFERRABLE INITIALLY IMMEDIATE');
$this->addSql('CREATE INDEX IDX_2FB3D0EEB10AD970 ON project (user_project_id)');
}

public function down(Schema $schema): void
{
$this->addSql('CREATE SCHEMA public');
$this->addSql('ALTER TABLE project DROP CONSTRAINT FK_2FB3D0EEB10AD970');
$this->addSql('DROP INDEX IDX_2FB3D0EEB10AD970');
$this->addSql('ALTER TABLE project DROP user_project_id');
}
}
21 changes: 18 additions & 3 deletions src/Entity/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class Project
#[ORM\ManyToMany(targetEntity: Category::class, mappedBy: 'projects', fetch: 'EAGER')]
private Collection $categories;

#[ORM\ManyToOne(inversedBy: 'projects')]
private ?User $userProject = null;

public function __construct()
{
$this->categories = new ArrayCollection();
Expand Down Expand Up @@ -108,12 +111,12 @@ final public function setUpdatedAtValue(): void
$this->updatedAt = new \DateTimeImmutable();
}

public function getCategories(): Collection
final public function getCategories(): Collection
{
return $this->categories;
}

public function addCategory(Category $category): self
final public function addCategory(Category $category): self
{
!$this->categories->contains($category)
&& $this->categories->add($category)
Expand All @@ -122,11 +125,23 @@ public function addCategory(Category $category): self
return $this;
}

public function removeCategory(Category $category): self
final public function removeCategory(Category $category): self
{
$this->categories->removeElement($category)
&& $category->removeProject($this);

return $this;
}

final public function getUserProject(): ?User
{
return $this->userProject;
}

final public function setUserProject(?User $userProject): self
{
$this->userProject = $userProject;

return $this;
}
}
26 changes: 17 additions & 9 deletions src/Entity/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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\Types\UuidType;
Expand Down Expand Up @@ -41,6 +42,14 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface
#[ORM\Column]
private ?string $password = null;

#[ORM\OneToMany(mappedBy: 'userProject', targetEntity: Project::class, fetch: 'EAGER')]
private Collection $projects;

public function __construct()
{
$this->projects = new ArrayCollection();
}

final public function getId(): ?Uuid
{
return $this->id;
Expand Down Expand Up @@ -100,27 +109,26 @@ final public function eraseCredentials(): void
// $this->plainPassword = null;
}

public function getProjects(): Collection
final public function getProjects(): Collection
{
return $this->projects;
}

public function addProject(Project $project): self
final public function addProject(Project $project): self
{
if (!$this->projects->contains($project)) {
$this->projects->add($project);
$project->setUser($this);
}
!$this->projects->contains($project)
&& $this->projects->add($project)
&& $project->setUserProject($this);

return $this;
}

public function removeProject(Project $project): self
final public function removeProject(Project $project): self
{
if ($this->projects->removeElement($project)) {
// set the owning side to null (unless already changed)
if ($project->getUser() === $this) {
$project->setUser(null);
if ($project->getUserProject() === $this) {
$project->setUserProject(null);
}
}

Expand Down

0 comments on commit e0bdb7c

Please sign in to comment.