public function preExecute() { $ad = $this->getRequest()->get('advertisement'); $institutionId = $ad ? $ad['institution'] : null; if ($advertisementId = $this->getRequest()->get('advertisementId', 0)) { $qb = $this->getDoctrine()->getEntityManager()->createQueryBuilder(); $qb->select('ad, a, b, c, d, e, f')->from('AdvertisementBundle:Advertisement', 'ad')->leftJoin('ad.institution', 'a')->leftJoin('a.institutionMedicalCenters', 'b')->leftJoin('b.institutionSpecializations', 'c')->leftJoin('c.specialization', 'd')->leftJoin('c.treatments', 'e')->leftJoin('e.subSpecializations', 'f')->where('ad.id = :advertisementId')->setParameter('advertisementId', $advertisementId); $this->advertisement = $qb->getQuery()->getOneOrNullResult(); if (!$this->advertisement) { throw $this->createNotFoundException("Invalid advertisement."); } $this->institution = $this->advertisement->getInstitution(); } if (($institutionId = $this->getRequest()->get('institutionId', $institutionId)) && !$this->advertisement) { $qb = $this->getDoctrine()->getEntityManager()->createQueryBuilder(); $qb->select('a, b, c, d, e, f')->from('InstitutionBundle:Institution', 'a')->leftJoin('a.institutionMedicalCenters', 'b')->leftJoin('b.institutionSpecializations', 'c')->leftJoin('c.specialization', 'd')->leftJoin('c.treatments', 'e')->leftJoin('e.subSpecializations', 'f')->where('a.id = :institutionId')->setParameter('institutionId', $institutionId); $this->institution = $qb->getQuery()->getOneOrNullResult(); if (!$this->institution) { throw $this->createNotFoundException("Invalid institution."); } } }
private function updateAdvertisementDenormalizedData(Advertisement $advertisement) { $data['id'] = $advertisement->getId(); $data['institution_id'] = $advertisement->getInstitution()->getId(); $data['advertisement_type_id'] = $advertisement->getAdvertisementType()->getId(); $data['title'] = $advertisement->getTitle(); $data['description'] = $advertisement->getDescription(); $data['date_created'] = $advertisement->getDateCreated() ? $advertisement->getDateCreated()->format('Y-m-d H:i:s') : 'now()'; $data['date_expiry'] = $advertisement->getDateExpiry()->format('Y-m-d H:i:s'); $data['status'] = $advertisement->getStatus(); // TODO - Enhance Saving Process. This can be done in 1 foreach. $collectionClasses = array(); foreach ($advertisement->getAdvertisementPropertyValues() as $each) { $property = $each->getAdvertisementPropertyName(); if ($property->getDataType()->getColumnType() == 'collection') { $data[$property->getName()][] = (int) $each->getValue(); $collectionClasses[$property->getName()] = $property->getDataClass(); } else { $data[$property->getName()] = $each->getValue(); } } $onDuplicatePlaceholder = $columns = $valuesPlaceholder = ''; foreach ($data as $key => $value) { if (is_array($value)) { if (empty($value)) { $data[$key] = ''; continue; } if (!$collectionClasses[$key]) { throw AdvertisementPropertyException::emptyPropertyClass($key); } $qb = $this->em->createQueryBuilder(); $query = $qb->select('a')->from($collectionClasses[$key], 'a')->where($qb->expr()->in('a.id', $value)); $data[$key] = json_encode($query->getQuery()->getResult(Query::HYDRATE_ARRAY)); } $columns .= ",{$key}"; $valuesPlaceholder .= ",:{$key}"; $onDuplicatePlaceholder .= ",{$key} = :{$key}"; } $columns = substr($columns, 1); $valuesPlaceholder = substr($valuesPlaceholder, 1); $onDuplicatePlaceholder = substr($onDuplicatePlaceholder, 1); $query = "INSERT INTO advertisement_denormalized_properties ({$columns}) VALUES({$valuesPlaceholder}) ON DUPLICATE KEY UPDATE {$onDuplicatePlaceholder}"; $result = $this->em->getConnection()->executeQuery($query, $data); }