From 14adb77a97a156a40e94c218e8a0049f1efd42ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Sat, 16 May 2020 20:53:35 +0200 Subject: [PATCH] Moved getParts() and getPartsCount() to a repository (instead of a class method). --- .../AdminPages/BaseAdminController.php | 13 +++- src/Controller/PartListsController.php | 13 ++++ .../Base/AbstractPartsContainingDBElement.php | 21 +----- .../PartsContainingRepositoryInterface.php | 43 ++++++++++++ src/Entity/Parts/Category.php | 7 +- src/Entity/Parts/Footprint.php | 6 +- src/Entity/Parts/Manufacturer.php | 6 +- src/Entity/Parts/MeasurementUnit.php | 6 +- src/Entity/Parts/PartLot.php | 2 +- .../Parts/PartTraits/BasicPropertyTrait.php | 4 +- src/Entity/Parts/PartTraits/InstockTrait.php | 2 +- .../Parts/PartTraits/ManufacturerTrait.php | 2 +- src/Entity/Parts/Storelocation.php | 11 +-- src/Entity/Parts/Supplier.php | 11 +-- .../AbstractPartsContainingRepository.php | 70 +++++++++++++++++++ src/Repository/Parts/CategoryRepository.php | 37 ++++++++++ src/Repository/Parts/FootprintRepository.php | 36 ++++++++++ .../Parts/ManufacturerRepository.php | 37 ++++++++++ .../Parts/MeasurementUnitRepository.php | 37 ++++++++++ .../Parts/StorelocationRepository.php | 59 ++++++++++++++++ src/Repository/Parts/SupplierRepository.php | 59 ++++++++++++++++ .../Constraints/ValidPartLotValidator.php | 4 +- templates/AdminPages/_info.html.twig | 4 +- templates/Parts/lists/_info_card.html.twig | 2 +- 24 files changed, 421 insertions(+), 71 deletions(-) create mode 100644 src/Entity/Base/PartsContainingRepositoryInterface.php create mode 100644 src/Repository/AbstractPartsContainingRepository.php create mode 100644 src/Repository/Parts/CategoryRepository.php create mode 100644 src/Repository/Parts/FootprintRepository.php create mode 100644 src/Repository/Parts/ManufacturerRepository.php create mode 100644 src/Repository/Parts/MeasurementUnitRepository.php create mode 100644 src/Repository/Parts/StorelocationRepository.php create mode 100644 src/Repository/Parts/SupplierRepository.php diff --git a/src/Controller/AdminPages/BaseAdminController.php b/src/Controller/AdminPages/BaseAdminController.php index fd86769c..da976cd6 100644 --- a/src/Controller/AdminPages/BaseAdminController.php +++ b/src/Controller/AdminPages/BaseAdminController.php @@ -45,6 +45,7 @@ namespace App\Controller\AdminPages; use App\DataTables\LogDataTable; use App\Entity\Base\AbstractNamedDBElement; use App\Entity\Base\AbstractStructuralDBElement; +use App\Entity\Base\PartsContainingRepositoryInterface; use App\Entity\LabelSystem\LabelProfile; use App\Entity\UserSystem\User; use App\Events\SecurityEvent; @@ -52,6 +53,7 @@ use App\Events\SecurityEvents; use App\Exceptions\AttachmentDownloadException; use App\Form\AdminPages\ImportType; use App\Form\AdminPages\MassCreationForm; +use App\Repository\AbstractPartsContainingRepository; use App\Services\Attachments\AttachmentSubmitHandler; use App\Services\EntityExporter; use App\Services\EntityImporter; @@ -100,11 +102,13 @@ abstract class BaseAdminController extends AbstractController protected $labelGenerator; protected $barcodeExampleGenerator; + protected $entityManager; + public function __construct(TranslatorInterface $translator, UserPasswordEncoderInterface $passwordEncoder, AttachmentSubmitHandler $attachmentSubmitHandler, EventCommentHelper $commentHelper, HistoryHelper $historyHelper, TimeTravel $timeTravel, DataTableFactory $dataTableFactory, EventDispatcherInterface $eventDispatcher, BarcodeExampleElementsGenerator $barcodeExampleGenerator, - LabelGenerator $labelGenerator) + LabelGenerator $labelGenerator, EntityManagerInterface $entityManager) { if ('' === $this->entity_class || '' === $this->form_class || '' === $this->twig_template || '' === $this->route_base) { throw new InvalidArgumentException('You have to override the $entity_class, $form_class, $route_base and $twig_template value in your subclasss!'); @@ -128,6 +132,7 @@ abstract class BaseAdminController extends AbstractController $this->eventDispatcher = $eventDispatcher; $this->barcodeExampleGenerator = $barcodeExampleGenerator; $this->labelGenerator = $labelGenerator; + $this->entityManager = $entityManager; } protected function _edit(AbstractNamedDBElement $entity, Request $request, EntityManagerInterface $em, ?string $timestamp = null): Response @@ -237,6 +242,10 @@ abstract class BaseAdminController extends AbstractController $pdf_data = $this->labelGenerator->generateLabel($entity->getOptions(), $example); } + /** @var AbstractPartsContainingRepository $repo */ + $repo = $this->entityManager->getRepository($this->entity_class); + + return $this->render($this->twig_template, [ 'entity' => $entity, 'form' => $form->createView(), @@ -244,6 +253,7 @@ abstract class BaseAdminController extends AbstractController 'datatable' => $table, 'pdf_data' => $pdf_data ?? null, 'timeTravel' => $timeTravel_timestamp, + 'repo' => $repo, ]); } @@ -357,6 +367,7 @@ abstract class BaseAdminController extends AbstractController $em->flush(); } + return $this->render($this->twig_template, [ 'entity' => $new_entity, 'form' => $form->createView(), diff --git a/src/Controller/PartListsController.php b/src/Controller/PartListsController.php index e525e0e0..73838577 100644 --- a/src/Controller/PartListsController.php +++ b/src/Controller/PartListsController.php @@ -48,6 +48,7 @@ use App\Entity\Parts\Footprint; use App\Entity\Parts\Manufacturer; use App\Entity\Parts\Storelocation; use App\Entity\Parts\Supplier; +use Doctrine\ORM\EntityManagerInterface; use Omines\DataTablesBundle\DataTableFactory; use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; use Symfony\Component\HttpFoundation\JsonResponse; @@ -57,6 +58,13 @@ use Symfony\Component\Routing\Annotation\Route; class PartListsController extends AbstractController { + private $entityManager; + + public function __construct(EntityManagerInterface $entityManager) + { + $this->entityManager = $entityManager; + } + /** * @Route("/category/{id}/parts", name="part_list_category") * @@ -74,6 +82,7 @@ class PartListsController extends AbstractController return $this->render('Parts/lists/category_list.html.twig', [ 'datatable' => $table, 'entity' => $category, + 'repo' => $this->entityManager->getRepository(Category::class), ]); } @@ -94,6 +103,7 @@ class PartListsController extends AbstractController return $this->render('Parts/lists/footprint_list.html.twig', [ 'datatable' => $table, 'entity' => $footprint, + 'repo' => $this->entityManager->getRepository(Footprint::class), ]); } @@ -114,6 +124,7 @@ class PartListsController extends AbstractController return $this->render('Parts/lists/manufacturer_list.html.twig', [ 'datatable' => $table, 'entity' => $manufacturer, + 'repo' => $this->entityManager->getRepository(Manufacturer::class), ]); } @@ -134,6 +145,7 @@ class PartListsController extends AbstractController return $this->render('Parts/lists/store_location_list.html.twig', [ 'datatable' => $table, 'entity' => $storelocation, + 'repo' => $this->entityManager->getRepository(Storelocation::class), ]); } @@ -154,6 +166,7 @@ class PartListsController extends AbstractController return $this->render('Parts/lists/supplier_list.html.twig', [ 'datatable' => $table, 'entity' => $supplier, + 'repo' => $this->entityManager->getRepository(Supplier::class), ]); } diff --git a/src/Entity/Base/AbstractPartsContainingDBElement.php b/src/Entity/Base/AbstractPartsContainingDBElement.php index 95270234..a813d1f3 100644 --- a/src/Entity/Base/AbstractPartsContainingDBElement.php +++ b/src/Entity/Base/AbstractPartsContainingDBElement.php @@ -30,28 +30,9 @@ use Doctrine\ORM\Mapping as ORM; /** * Class PartsContainingDBElement. * - * @ORM\MappedSuperclass() + * @ORM\MappedSuperclass(repositoryClass="App\Repository\AbstractPartsContainingRepository") */ abstract class AbstractPartsContainingDBElement extends AbstractStructuralDBElement { - /** - * @var Part[]|Collection - */ - protected $parts; - public function __construct() - { - parent::__construct(); - $this->parts = new ArrayCollection(); - } - - /** - * Returns the parts associated with this element. - * - * @return Collection|Part[] - */ - public function getParts(): Collection - { - return $this->parts; - } } diff --git a/src/Entity/Base/PartsContainingRepositoryInterface.php b/src/Entity/Base/PartsContainingRepositoryInterface.php new file mode 100644 index 00000000..bcdaa07a --- /dev/null +++ b/src/Entity/Base/PartsContainingRepositoryInterface.php @@ -0,0 +1,43 @@ +. + */ + +namespace App\Entity\Base; + + +use App\Entity\Parts\Part; +use Doctrine\Common\Collections\Collection; + +interface PartsContainingRepositoryInterface +{ + /** + * Returns all parts associated with this element. + * @param object $element The element for which the parts should be determined. + * @param array $order_by The order of the parts. Format ['name' => 'ASC'] + * @return Part[] + */ + public function getParts(object $element, array $order_by = ['name' => 'ASC']): array; + + /** + * Gets the count of the parts associated with this element. + * @param object $element The element for which the parts should be determined. + * @return int + */ + public function getPartsCount(object $element): int; +} \ No newline at end of file diff --git a/src/Entity/Parts/Category.php b/src/Entity/Parts/Category.php index 551778df..e0fee90f 100644 --- a/src/Entity/Parts/Category.php +++ b/src/Entity/Parts/Category.php @@ -32,7 +32,7 @@ use Symfony\Component\Validator\Constraints as Assert; /** * Class AttachmentType. * - * @ORM\Entity(repositoryClass="App\Repository\StructuralDBElementRepository") + * @ORM\Entity(repositoryClass="App\Repository\Parts\CategoryRepository") * @ORM\Table(name="`categories`") */ class Category extends AbstractPartsContainingDBElement @@ -49,11 +49,6 @@ class Category extends AbstractPartsContainingDBElement */ protected $parent; - /** - * @ORM\OneToMany(targetEntity="Part", mappedBy="category", fetch="EXTRA_LAZY") - */ - protected $parts; - /** * @var string * @ORM\Column(type="text") diff --git a/src/Entity/Parts/Footprint.php b/src/Entity/Parts/Footprint.php index a648afce..246ca4e3 100644 --- a/src/Entity/Parts/Footprint.php +++ b/src/Entity/Parts/Footprint.php @@ -60,7 +60,7 @@ use Symfony\Component\Validator\Constraints as Assert; /** * Class Footprint. * - * @ORM\Entity(repositoryClass="App\Repository\StructuralDBElementRepository") + * @ORM\Entity(repositoryClass="App\Repository\Parts\FootprintRepository") * @ORM\Table("`footprints`") */ class Footprint extends AbstractPartsContainingDBElement @@ -77,10 +77,6 @@ class Footprint extends AbstractPartsContainingDBElement */ protected $children; - /** - * @ORM\OneToMany(targetEntity="Part", mappedBy="footprint", fetch="EXTRA_LAZY") - */ - protected $parts; /** * @var Collection * @ORM\OneToMany(targetEntity="App\Entity\Attachments\FootprintAttachment", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true) diff --git a/src/Entity/Parts/Manufacturer.php b/src/Entity/Parts/Manufacturer.php index ea8434c2..90a14999 100644 --- a/src/Entity/Parts/Manufacturer.php +++ b/src/Entity/Parts/Manufacturer.php @@ -60,7 +60,7 @@ use Symfony\Component\Validator\Constraints as Assert; /** * Class Manufacturer. * - * @ORM\Entity(repositoryClass="App\Repository\StructuralDBElementRepository") + * @ORM\Entity(repositoryClass="App\Repository\Parts\ManufacturerRepository") * @ORM\Table("`manufacturers`") */ class Manufacturer extends AbstractCompany @@ -77,10 +77,6 @@ class Manufacturer extends AbstractCompany */ protected $children; - /** - * @ORM\OneToMany(targetEntity="Part", mappedBy="manufacturer", fetch="EXTRA_LAZY") - */ - protected $parts; /** * @var Collection * @ORM\OneToMany(targetEntity="App\Entity\Attachments\ManufacturerAttachment", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true) diff --git a/src/Entity/Parts/MeasurementUnit.php b/src/Entity/Parts/MeasurementUnit.php index fc8f770a..7ffa238a 100644 --- a/src/Entity/Parts/MeasurementUnit.php +++ b/src/Entity/Parts/MeasurementUnit.php @@ -54,7 +54,7 @@ use Symfony\Component\Validator\Constraints as Assert; * This unit represents the unit in which the amount of parts in stock are measured. * This could be something like N, grams, meters, etc... * - * @ORM\Entity(repositoryClass="App\Repository\StructuralDBElementRepository") + * @ORM\Entity(repositoryClass="App\Repository\Parts\MeasurementUnitRepository") * @ORM\Table(name="`measurement_units`") * @UniqueEntity("unit") */ @@ -94,10 +94,6 @@ class MeasurementUnit extends AbstractPartsContainingDBElement */ protected $parent; - /** - * @ORM\OneToMany(targetEntity="Part", mappedBy="partUnit", fetch="EXTRA_LAZY") - */ - protected $parts; /** * @var Collection * @ORM\OneToMany(targetEntity="App\Entity\Attachments\MeasurementUnitAttachment", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true) diff --git a/src/Entity/Parts/PartLot.php b/src/Entity/Parts/PartLot.php index 67234d73..73d7d2d6 100644 --- a/src/Entity/Parts/PartLot.php +++ b/src/Entity/Parts/PartLot.php @@ -114,7 +114,7 @@ class PartLot extends AbstractDBElement implements TimeStampableInterface, Named /** * @var Part The part that is stored in this lot - * @ORM\ManyToOne(targetEntity="Part", inversedBy="partLots") + * @ORM\ManyToOne(targetEntity="Part") * @ORM\JoinColumn(name="id_part", referencedColumnName="id", nullable=false, onDelete="CASCADE") * @Assert\NotNull() */ diff --git a/src/Entity/Parts/PartTraits/BasicPropertyTrait.php b/src/Entity/Parts/PartTraits/BasicPropertyTrait.php index 9eeab4ee..57941140 100644 --- a/src/Entity/Parts/PartTraits/BasicPropertyTrait.php +++ b/src/Entity/Parts/PartTraits/BasicPropertyTrait.php @@ -79,7 +79,7 @@ trait BasicPropertyTrait /** * @var Category The category this part belongs too (e.g. Resistors). Use tags, for more complex grouping. * Every part must have a category. - * @ORM\ManyToOne(targetEntity="Category", inversedBy="parts") + * @ORM\ManyToOne(targetEntity="Category") * @ORM\JoinColumn(name="id_category", referencedColumnName="id", nullable=false) * @ColumnSecurity(prefix="category", type="App\Entity\Parts\Category") * @Selectable() @@ -88,7 +88,7 @@ trait BasicPropertyTrait /** * @var Footprint|null The footprint of this part (e.g. DIP8) - * @ORM\ManyToOne(targetEntity="Footprint", inversedBy="parts") + * @ORM\ManyToOne(targetEntity="Footprint") * @ORM\JoinColumn(name="id_footprint", referencedColumnName="id") * @ColumnSecurity(prefix="footprint", type="App\Entity\Parts\Footprint") * @Selectable() diff --git a/src/Entity/Parts/PartTraits/InstockTrait.php b/src/Entity/Parts/PartTraits/InstockTrait.php index 2eb596eb..8f7516bd 100644 --- a/src/Entity/Parts/PartTraits/InstockTrait.php +++ b/src/Entity/Parts/PartTraits/InstockTrait.php @@ -72,7 +72,7 @@ trait InstockTrait /** * @var ?MeasurementUnit the unit in which the part's amount is measured - * @ORM\ManyToOne(targetEntity="MeasurementUnit", inversedBy="parts") + * @ORM\ManyToOne(targetEntity="MeasurementUnit") * @ORM\JoinColumn(name="id_part_unit", referencedColumnName="id", nullable=true) * @ColumnSecurity(type="object", prefix="unit") */ diff --git a/src/Entity/Parts/PartTraits/ManufacturerTrait.php b/src/Entity/Parts/PartTraits/ManufacturerTrait.php index 0717e45e..78437328 100644 --- a/src/Entity/Parts/PartTraits/ManufacturerTrait.php +++ b/src/Entity/Parts/PartTraits/ManufacturerTrait.php @@ -54,7 +54,7 @@ trait ManufacturerTrait { /** * @var Manufacturer|null The manufacturer of this part - * @ORM\ManyToOne(targetEntity="Manufacturer", inversedBy="parts") + * @ORM\ManyToOne(targetEntity="Manufacturer") * @ORM\JoinColumn(name="id_manufacturer", referencedColumnName="id") * @ColumnSecurity(prefix="manufacturer", type="App\Entity\Parts\Manufacturer") * @Selectable() diff --git a/src/Entity/Parts/Storelocation.php b/src/Entity/Parts/Storelocation.php index c71a4140..a2e58c0a 100644 --- a/src/Entity/Parts/Storelocation.php +++ b/src/Entity/Parts/Storelocation.php @@ -60,7 +60,7 @@ use Symfony\Component\Validator\Constraints as Assert; /** * Class Store location. * - * @ORM\Entity(repositoryClass="App\Repository\StructuralDBElementRepository") + * @ORM\Entity(repositoryClass="App\Repository\Parts\StorelocationRepository") * @ORM\Table("`storelocations`") */ class Storelocation extends AbstractPartsContainingDBElement @@ -84,15 +84,6 @@ class Storelocation extends AbstractPartsContainingDBElement */ protected $storage_type; - /** - * @ORM\ManyToMany(targetEntity="Part", fetch="EXTRA_LAZY") - * @ORM\JoinTable(name="part_lots", - * joinColumns={@ORM\JoinColumn(name="id_store_location", referencedColumnName="id")}, - * inverseJoinColumns={@ORM\JoinColumn(name="id_part", referencedColumnName="id")} - * ) - */ - protected $parts; - /** @var Collection * @ORM\OneToMany(targetEntity="App\Entity\Parameters\StorelocationParameter", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true) * @ORM\OrderBy({"group" = "ASC" ,"name" = "ASC"}) diff --git a/src/Entity/Parts/Supplier.php b/src/Entity/Parts/Supplier.php index 2a9a2a22..b2c464a5 100644 --- a/src/Entity/Parts/Supplier.php +++ b/src/Entity/Parts/Supplier.php @@ -62,7 +62,7 @@ use Symfony\Component\Validator\Constraints as Assert; /** * Class Supplier. * - * @ORM\Entity(repositoryClass="App\Repository\StructuralDBElementRepository") + * @ORM\Entity(repositoryClass="App\Repository\Parts\SupplierRepository") * @ORM\Table("`suppliers`") */ class Supplier extends AbstractCompany @@ -100,15 +100,6 @@ class Supplier extends AbstractCompany */ protected $shipping_costs; - /** - * @ORM\ManyToMany(targetEntity="Part", fetch="EXTRA_LAZY") - * @ORM\JoinTable(name="orderdetails", - * joinColumns={@ORM\JoinColumn(name="id_supplier", referencedColumnName="id")}, - * inverseJoinColumns={@ORM\JoinColumn(name="part_id", referencedColumnName="id")} - * ) - */ - protected $parts; - /** * @var Collection * @ORM\OneToMany(targetEntity="App\Entity\Attachments\SupplierAttachment", mappedBy="element", cascade={"persist", "remove"}, orphanRemoval=true) diff --git a/src/Repository/AbstractPartsContainingRepository.php b/src/Repository/AbstractPartsContainingRepository.php new file mode 100644 index 00000000..0c1f1dba --- /dev/null +++ b/src/Repository/AbstractPartsContainingRepository.php @@ -0,0 +1,70 @@ +. + */ + +namespace App\Repository; + + +use App\Entity\Base\AbstractPartsContainingDBElement; +use App\Entity\Base\PartsContainingRepositoryInterface; +use App\Entity\Parts\Part; +use Doctrine\Common\Collections\Collection; +use Doctrine\Common\Collections\Criteria; + +abstract class AbstractPartsContainingRepository extends StructuralDBElementRepository + implements PartsContainingRepositoryInterface +{ + /** + * Returns all parts associated with this element. + * @param AbstractPartsContainingDBElement $element The element for which the parts should be determined. + * @param array $order_by The order of the parts. Format ['name' => 'ASC'] + * @return Part[] + */ + abstract public function getParts(object $element, array $order_by = ['name' => 'ASC']): array; + + /** + * Gets the count of the parts associated with this element. + * @param AbstractPartsContainingDBElement $element The element for which the parts should be determined. + * @return int + */ + abstract public function getPartsCount(object $element): int; + + protected function getPartsByField(object $element, array $order_by, string $field_name): array + { + if (!$element instanceof AbstractPartsContainingDBElement) { + throw new \InvalidArgumentException('$element must be an instance of AbstractPartContainingDBElement!'); + } + + $repo = $this->getEntityManager()->getRepository(Part::class); + + return $repo->findBy([$field_name => $element], $order_by); + } + + protected function getPartsCountByField(object $element, string $field_name): int + { + if (!$element instanceof AbstractPartsContainingDBElement) { + throw new \InvalidArgumentException('$element must be an instance of AbstractPartContainingDBElement!'); + } + + $repo = $this->getEntityManager()->getRepository(Part::class); + + return $repo->count([$field_name => $element]); + } + +} \ No newline at end of file diff --git a/src/Repository/Parts/CategoryRepository.php b/src/Repository/Parts/CategoryRepository.php new file mode 100644 index 00000000..53a075f6 --- /dev/null +++ b/src/Repository/Parts/CategoryRepository.php @@ -0,0 +1,37 @@ +. + */ + +namespace App\Repository\Parts; + +use App\Repository\AbstractPartsContainingRepository; + +class CategoryRepository extends AbstractPartsContainingRepository +{ + + public function getParts(object $element, array $order_by = ['name' => 'ASC']): array + { + return $this->getPartsByField($element, $order_by, 'category'); + } + + public function getPartsCount(object $element): int + { + return $this->getPartsCountByField($element, 'category'); + } +} \ No newline at end of file diff --git a/src/Repository/Parts/FootprintRepository.php b/src/Repository/Parts/FootprintRepository.php new file mode 100644 index 00000000..a80e4bff --- /dev/null +++ b/src/Repository/Parts/FootprintRepository.php @@ -0,0 +1,36 @@ +. + */ + +namespace App\Repository\Parts; + +use App\Repository\AbstractPartsContainingRepository; + +class FootprintRepository extends AbstractPartsContainingRepository +{ + public function getParts(object $element, array $order_by = ['name' => 'ASC']): array + { + return $this->getPartsByField($element, $order_by, 'footprint'); + } + + public function getPartsCount(object $element): int + { + return $this->getPartsCountByField($element, 'footprint'); + } +} \ No newline at end of file diff --git a/src/Repository/Parts/ManufacturerRepository.php b/src/Repository/Parts/ManufacturerRepository.php new file mode 100644 index 00000000..f2a1708b --- /dev/null +++ b/src/Repository/Parts/ManufacturerRepository.php @@ -0,0 +1,37 @@ +. + */ + +namespace App\Repository\Parts; + +use App\Repository\AbstractPartsContainingRepository; + +class ManufacturerRepository extends AbstractPartsContainingRepository +{ + + public function getParts(object $element, array $order_by = ['name' => 'ASC']): array + { + return $this->getPartsByField($element, $order_by, 'manufacturer'); + } + + public function getPartsCount(object $element): int + { + return $this->getPartsCountByField($element, 'manufacturer'); + } +} \ No newline at end of file diff --git a/src/Repository/Parts/MeasurementUnitRepository.php b/src/Repository/Parts/MeasurementUnitRepository.php new file mode 100644 index 00000000..9f783354 --- /dev/null +++ b/src/Repository/Parts/MeasurementUnitRepository.php @@ -0,0 +1,37 @@ +. + */ + +namespace App\Repository\Parts; + +use App\Repository\AbstractPartsContainingRepository; + +class MeasurementUnitRepository extends AbstractPartsContainingRepository +{ + + public function getParts(object $element, array $order_by = ['name' => 'ASC']): array + { + return $this->getPartsByField($element, $order_by, 'partUnit'); + } + + public function getPartsCount(object $element): int + { + return $this->getPartsCountByField($element, 'partUnit'); + } +} \ No newline at end of file diff --git a/src/Repository/Parts/StorelocationRepository.php b/src/Repository/Parts/StorelocationRepository.php new file mode 100644 index 00000000..970445b4 --- /dev/null +++ b/src/Repository/Parts/StorelocationRepository.php @@ -0,0 +1,59 @@ +. + */ + +namespace App\Repository\Parts; + +use App\Entity\Parts\Part; +use App\Repository\AbstractPartsContainingRepository; +use Doctrine\ORM\QueryBuilder; + +class StorelocationRepository extends AbstractPartsContainingRepository +{ + public function getParts(object $element, array $order_by = ['name' => 'ASC']): array + { + $qb = new QueryBuilder($this->getEntityManager()); + + $qb->select('part') + ->from(Part::class, 'part') + ->leftJoin('part.partLots', 'lots') + ->where('lots.storage_location = ?1') + ->setParameter(1, $element); + + foreach ($order_by as $field => $order) { + $qb->addOrderBy('part.' . $field, $order); + } + + return $qb->getQuery()->getResult(); + } + + public function getPartsCount(object $element): int + { + $qb = new QueryBuilder($this->getEntityManager()); + + $qb->select('COUNT(part.id)') + ->from(Part::class, 'part') + ->leftJoin('part.partLots', 'lots') + ->where('lots.storage_location = ?1') + ->setParameter(1, $element); + + + return (int) $qb->getQuery()->getSingleScalarResult(); + } +} \ No newline at end of file diff --git a/src/Repository/Parts/SupplierRepository.php b/src/Repository/Parts/SupplierRepository.php new file mode 100644 index 00000000..515a9335 --- /dev/null +++ b/src/Repository/Parts/SupplierRepository.php @@ -0,0 +1,59 @@ +. + */ + +namespace App\Repository\Parts; + +use App\Entity\Parts\Part; +use App\Repository\AbstractPartsContainingRepository; +use Doctrine\ORM\QueryBuilder; + +class SupplierRepository extends AbstractPartsContainingRepository +{ + public function getParts(object $element, array $order_by = ['name' => 'ASC']): array + { + $qb = new QueryBuilder($this->getEntityManager()); + + $qb->select('part') + ->from(Part::class, 'part') + ->leftJoin('part.orderdetails', 'orderdetail') + ->where('orderdetail.supplier = ?1') + ->setParameter(1, $element); + + foreach ($order_by as $field => $order) { + $qb->addOrderBy('part.' . $field, $order); + } + + return $qb->getQuery()->getResult(); + } + + public function getPartsCount(object $element): int + { + $qb = new QueryBuilder($this->getEntityManager()); + + $qb->select('COUNT(part.id)') + ->from(Part::class, 'part') + ->leftJoin('part.orderdetails', 'orderdetail') + ->where('orderdetail.supplier = ?1') + ->setParameter(1, $element); + + + return (int) $qb->getQuery()->getSingleScalarResult(); + } +} \ No newline at end of file diff --git a/src/Validator/Constraints/ValidPartLotValidator.php b/src/Validator/Constraints/ValidPartLotValidator.php index 62ab9000..e89b004c 100644 --- a/src/Validator/Constraints/ValidPartLotValidator.php +++ b/src/Validator/Constraints/ValidPartLotValidator.php @@ -43,6 +43,7 @@ declare(strict_types=1); namespace App\Validator\Constraints; use App\Entity\Parts\PartLot; +use App\Entity\Parts\Storelocation; use Doctrine\ORM\EntityManagerInterface; use Symfony\Component\Form\Exception\UnexpectedTypeException; use Symfony\Component\Validator\Constraint; @@ -75,7 +76,8 @@ class ValidPartLotValidator extends ConstraintValidator //We can only validate the values if we know the storelocation if ($value->getStorageLocation()) { - $parts = $value->getStorageLocation()->getParts(); + $repo = $this->em->getRepository(Storelocation::class); + $parts = $repo->getParts($value); //Check for isFull() attribute if ($value->getStorageLocation()->isFull()) { diff --git a/templates/AdminPages/_info.html.twig b/templates/AdminPages/_info.html.twig index a6b01460..c4fa7412 100644 --- a/templates/AdminPages/_info.html.twig +++ b/templates/AdminPages/_info.html.twig @@ -38,8 +38,8 @@

- {% if entity.id and entity.parts is defined %} - {{ entity.parts | length }} + {% if entity.id and repo.partsCount is defined %} + {{ repo.partsCount(entity) }} {% else %} - {% endif %} diff --git a/templates/Parts/lists/_info_card.html.twig b/templates/Parts/lists/_info_card.html.twig index a73196aa..8f5d633e 100644 --- a/templates/Parts/lists/_info_card.html.twig +++ b/templates/Parts/lists/_info_card.html.twig @@ -94,7 +94,7 @@

- {{ entity.parts | length }} + {{ repo.partsCount(entity) }}