/** * EAV optimization: fetching all values at the same time * * @return array|\Traversable */ public function getResults() { /** @var ArrayIterator $datas */ $datas = $this->getPager()->getCurrentPageResults(); /** @var EntityRepository $repo */ $repo = $this->doctrine->getRepository($this->family->getDataClass()); // No need to actually fetch the results, the already existing data will be hydrated automatically $repo->createQueryBuilder('d')->addSelect('v')->leftJoin('d.values', 'v')->where('d.id IN (:datas)')->setParameter('datas', is_array($datas) ? $datas : $datas->getArrayCopy())->getQuery()->getResult(); return $datas; }
/** * Return singleton for a given family * * @param FamilyInterface $family * * @throws \LogicException * @throws \Doctrine\ORM\NonUniqueResultException * * @return DataInterface */ public function getInstance(FamilyInterface $family) { if (!$family->isSingleton()) { throw new \LogicException("Family {$family->getCode()} is not a singleton"); } $qb = $this->createQueryBuilder('d')->andWhere('d.family = :familyCode')->addSelect('values')->join('d.values', 'values')->setParameters(['familyCode' => $family->getCode()]); $instance = $qb->getQuery()->getOneOrNullResult(); if (!$instance) { $dataClass = $family->getDataClass(); $instance = new $dataClass($family); } return $instance; }
/** * @param $id * @param FamilyInterface|null $family * @return Data * @throws Exception */ protected function getData($id, FamilyInterface $family = null) { if ($id instanceof Data) { $data = $id; } else { /** @var Data $data */ if ($family) { $data = $this->getDoctrine()->getRepository($family->getDataClass())->find($id); } else { $data = $this->container->get('sidus_eav_model.doctrine.repository.data')->find($id); } if (!$data) { throw new NotFoundHttpException("No data found with id : {$id}"); } } if (!$family) { $family = $data->getFamily(); } $this->initDataFamily($family, $data); return $data; }
/** * @param FamilyInterface $family * * @return string */ protected function getFileHeader(FamilyInterface $family) { $content = <<<EOT <?php namespace Sidus\\EAV; abstract class {$family->getCode()} extends EOT; if ($family->getParent()) { $content .= $family->getParent()->getCode(); } else { $content .= '\\' . $family->getDataClass(); } $content .= "\n{\n"; return $content; }
/** * @param FamilyInterface $parent */ protected function copyFromFamily(FamilyInterface $parent) { foreach ($parent->getAttributes() as $attribute) { $this->addAttribute($attribute); } $this->attributeAsLabel = $parent->getAttributeAsLabel(); $this->attributeAsIdentifier = $parent->getAttributeAsIdentifier(); $this->valueClass = $parent->getValueClass(); $this->dataClass = $parent->getDataClass(); }