Let categories contain other categories, and share them with resources
Two gaps against the spec. Resources could not be categorised at all — only services carried a catalog category — so "this device is for hands and feet" was unsayable. And CatalogCategory::$parent is a tree built for menu ordering: one parent per category. Laser areas overlap, so "hand" belongs under both "whole body" and "upper limb" at once, which a tree cannot express. Containment is therefore a separate directed acyclic graph (catalog_category_includes) sitting beside the display hierarchy, and resources join the existing clinic-wide categories through a many-to-many rather than growing a parallel list of their own. CategoryClosureResolver walks it transitively: whole body includes lower body includes foot, so whole body includes foot without anyone writing that pair down. The walk reads every edge of the environment in one query and traverses in memory — a query per level would tie round-trips to graph depth. The visited set doubles as the cycle guard, so even data that already contains a loop cannot hang the traversal, and assertNoCycle refuses to create one. Selection now rejects picking an area together with a category that contains it: "whole body laser" and "hand laser" in one appointment is a 422 with a Persian message naming both. This replaces hand-written incompatible_with pairs for the area case — defined once on the category instead of per item pair — while that relation stays for incompatibilities that have nothing to do with areas. Nine tests, including the two-parents case a tree could not hold, the cycle refusal, the self-edge, and the empty-graph boundary. TenantSchemaCoverageTest caught the new edge entity as unclassified; it is registered as an aggregate child of the parent category, which is what the constructor already enforces. Suite 1286 green, phpstan at its 14-error baseline. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
|
||||
namespace App\ClinicService\Repository;
|
||||
|
||||
use App\ClinicService\Entity\CatalogCategory;
|
||||
use App\ClinicService\Entity\CatalogCategoryInclude;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
/**
|
||||
* @extends ServiceEntityRepository<CatalogCategoryInclude>
|
||||
*/
|
||||
class CatalogCategoryIncludeRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, CatalogCategoryInclude::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* یالهای مستقیمِ یک محیط، بهصورت نگاشت والد => فرزندان.
|
||||
*
|
||||
* همهٔ یالهای محیط با **یک** کوئری خوانده میشوند و پیمایش در حافظه انجام میشود:
|
||||
* بستار گذرا با یک کوئری per سطح یعنی تعداد رفتوبرگشت به عمق گراف وابسته شود.
|
||||
*
|
||||
* @return array<int, list<int>>
|
||||
*/
|
||||
public function edgeMapFor(string $entityType, int $entityId): array
|
||||
{
|
||||
$rows = $this->createQueryBuilder('e')
|
||||
->select('IDENTITY(e.parent) AS parent_id, IDENTITY(e.child) AS child_id')
|
||||
->join('e.parent', 'p')
|
||||
->where('p.entityType = :type')
|
||||
->andWhere('p.entityId = :id')
|
||||
->setParameter('type', $entityType)
|
||||
->setParameter('id', $entityId)
|
||||
->getQuery()
|
||||
->getArrayResult();
|
||||
|
||||
$map = [];
|
||||
foreach ($rows as $row) {
|
||||
$map[(int) $row['parent_id']][] = (int) $row['child_id'];
|
||||
}
|
||||
|
||||
return $map;
|
||||
}
|
||||
|
||||
public function findEdge(CatalogCategory $parent, CatalogCategory $child): ?CatalogCategoryInclude
|
||||
{
|
||||
return $this->findOneBy(['parent' => $parent, 'child' => $child]);
|
||||
}
|
||||
|
||||
/** @return CatalogCategoryInclude[] */
|
||||
public function findForParent(CatalogCategory $parent): array
|
||||
{
|
||||
return $this->createQueryBuilder('e')
|
||||
->join('e.child', 'c')
|
||||
->addSelect('c')
|
||||
->where('e.parent = :parent')
|
||||
->setParameter('parent', $parent)
|
||||
->orderBy('c.name', 'ASC')
|
||||
->getQuery()
|
||||
->getResult();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user