From 57c8142fc9a77c264ef10e729bd44e62ae4b1f69 Mon Sep 17 00:00:00 2001 From: Daniel Subiabre Date: Wed, 6 Mar 2024 16:19:28 +0100 Subject: [PATCH] Tighten link between Accountings and owner entities --- src/Entity/Accounting.php | 22 +++++++++++ src/Entity/Project.php | 3 +- src/Entity/User.php | 4 +- src/Repository/ProjectRepository.php | 56 ++++++++++++++++------------ src/Repository/UserRepository.php | 10 +++++ 5 files changed, 70 insertions(+), 25 deletions(-) diff --git a/src/Entity/Accounting.php b/src/Entity/Accounting.php index e5c8f4a..5beb044 100644 --- a/src/Entity/Accounting.php +++ b/src/Entity/Accounting.php @@ -40,8 +40,18 @@ class Accounting #[ORM\OneToMany(mappedBy: 'target', targetEntity: Transaction::class)] private Collection $transactionsReceived; + #[API\ApiProperty(writable: false, readable: false)] + #[ORM\Column(length: 255)] + private ?string $ownerClass = null; + public function __construct() { + /** + * TODO: This property must be loaded from App's configuration, + * ideally a configuration that can be updated via a frontend, not env var only + */ + $this->currency = 'EUR'; + $this->transactionsIssued = new ArrayCollection(); $this->transactionsReceived = new ArrayCollection(); } @@ -122,4 +132,16 @@ class Accounting return $this; } + + public function getOwnerClass(): ?string + { + return $this->ownerClass; + } + + public function setOwnerClass(string $ownerClass): static + { + $this->ownerClass = $ownerClass; + + return $this; + } } diff --git a/src/Entity/Project.php b/src/Entity/Project.php index 465813e..b0faef1 100644 --- a/src/Entity/Project.php +++ b/src/Entity/Project.php @@ -36,7 +36,8 @@ class Project public function __construct() { - $this->accounting = new Accounting; + $this->accounting = new Accounting(); + $this->accounting->setOwnerClass(Project::class); } public function getId(): ?int diff --git a/src/Entity/User.php b/src/Entity/User.php index 6c7faf8..082d38e 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -86,7 +86,9 @@ class User implements UserInterface, PasswordAuthenticatedUserInterface public function __construct() { - $this->accounting = new Accounting; + $this->accounting = new Accounting(); + $this->accounting->setOwnerClass(User::class); + $this->accessTokens = new ArrayCollection(); } diff --git a/src/Repository/ProjectRepository.php b/src/Repository/ProjectRepository.php index 477b761..a1a9495 100644 --- a/src/Repository/ProjectRepository.php +++ b/src/Repository/ProjectRepository.php @@ -2,6 +2,7 @@ namespace App\Repository; +use App\Entity\Accounting; use App\Entity\Project; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Persistence\ManagerRegistry; @@ -21,28 +22,37 @@ class ProjectRepository extends ServiceEntityRepository parent::__construct($registry, Project::class); } -// /** -// * @return Project[] Returns an array of Project objects -// */ -// public function findByExampleField($value): array -// { -// return $this->createQueryBuilder('p') -// ->andWhere('p.exampleField = :val') -// ->setParameter('val', $value) -// ->orderBy('p.id', 'ASC') -// ->setMaxResults(10) -// ->getQuery() -// ->getResult() -// ; -// } + public function findOneByAccounting(Accounting $accounting): ?Project + { + return $this->createQueryBuilder('p') + ->andWhere('p.accounting = :val') + ->setParameter('val', $accounting->getId()) + ->getQuery() + ->getOneOrNullResult(); + } -// public function findOneBySomeField($value): ?Project -// { -// return $this->createQueryBuilder('p') -// ->andWhere('p.exampleField = :val') -// ->setParameter('val', $value) -// ->getQuery() -// ->getOneOrNullResult() -// ; -// } + // /** + // * @return Project[] Returns an array of Project objects + // */ + // public function findByExampleField($value): array + // { + // return $this->createQueryBuilder('p') + // ->andWhere('p.exampleField = :val') + // ->setParameter('val', $value) + // ->orderBy('p.id', 'ASC') + // ->setMaxResults(10) + // ->getQuery() + // ->getResult() + // ; + // } + + // public function findOneBySomeField($value): ?Project + // { + // return $this->createQueryBuilder('p') + // ->andWhere('p.exampleField = :val') + // ->setParameter('val', $value) + // ->getQuery() + // ->getOneOrNullResult() + // ; + // } } diff --git a/src/Repository/UserRepository.php b/src/Repository/UserRepository.php index 50a7511..9bc97fa 100644 --- a/src/Repository/UserRepository.php +++ b/src/Repository/UserRepository.php @@ -2,6 +2,7 @@ namespace App\Repository; +use App\Entity\Accounting; use App\Entity\User; use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository; use Doctrine\Persistence\ManagerRegistry; @@ -40,6 +41,15 @@ class UserRepository extends ServiceEntityRepository implements PasswordUpgrader $this->getEntityManager()->flush(); } + public function findOneByAccounting(Accounting $accounting): ?User + { + return $this->createQueryBuilder('u') + ->andWhere('u.accounting = :val') + ->setParameter('val', $accounting->getId()) + ->getQuery() + ->getOneOrNullResult(); + } + // /** // * @return User[] Returns an array of User objects // */