/** * @param Request $request * @param FilterInterface $filter * @param Criteria $criteria * @param ClassMetadata $embedClassMeta * * @return null */ protected function applyFilter(Request $request, FilterInterface $filter, Criteria $criteria, ClassMetadata $embedClassMeta) { $properties = $filter->getRequestProperties($request); if ($filter instanceof OrderFilter && !empty($properties)) { $criteria->orderBy($properties); return null; } if ($filter instanceof SearchFilter) { foreach ($properties as $name => $propertie) { if (in_array($name, $embedClassMeta->getIdentifier())) { continue; } $expCriterial = Criteria::expr(); if ($embedClassMeta->hasAssociation($name)) { $associationTargetClass = $embedClassMeta->getAssociationTargetClass($name); $propertyResource = $this->resourceResolver->getResourceForEntity($associationTargetClass); $propertyObj = $this->dataProviderChain->getItem($propertyResource, (int) $propertie['value'], true); if ($propertyObj && $propertyResource instanceof ResourceInterface) { $whereCriteria = $expCriterial->in($name, [$propertyObj]); $criteria->where($whereCriteria); } } else { if ($embedClassMeta->hasField($name)) { $fieldMapping = $embedClassMeta->getFieldMapping($name); $type = isset($fieldMapping['type']) ? $fieldMapping['type'] : null; $value = isset($this->mappingFilterVar[$type]) ? filter_var($propertie['value'], $this->mappingFilterVar[$type]) : $propertie['value']; $whereCriteria = isset($propertie['precision']) && $propertie['precision'] === 'exact' ? $expCriterial->eq($name, $value) : $expCriterial->contains($name, $propertie['value']); $criteria->where($whereCriteria); } } } } }
/** * @param Criteria $criteria */ public function count(Criteria $criteria) { $identifiers = $this->metadata->getIdentifier(); $identifierName = reset($identifiers); $qb = $this->repository->createQueryBuilder('e'); $this->buildWhereClause($qb, $criteria); return $qb->select("count(e.{$identifierName})")->getQuery()->getSingleScalarResult(); }
function it_schedules_owning_document_for_update_when_setting_element_by_key_in_the_collection(MongoDBODMUnitOfWork $uow, DocumentStub $document, ObjectRepository $repository, ClassMetadata $classMetadata, EntityStub $entity4, EntityStub $entity8, EntityStub $entity15, EntityStub $newEntity) { $classMetadata->getIdentifier()->willReturn(['id']); $repository->findBy(['id' => [4, 8, 15]])->willReturn([$entity4, $entity8, $entity15]); $uow->getDocumentState($document)->willReturn(MongoDBODMUnitOfWork::STATE_MANAGED); $uow->isScheduledForUpdate($document)->willReturn(false); $uow->scheduleForUpdate($document)->shouldBeCalled(); $this->setOwner($document); $this->set(2, $newEntity); }
public function fieldList(ClassMetadata $metadata, $includeId = true) { $return = []; $serializerMetadata = $metadata->getSerializer(); foreach ($metadata->getFieldNames() as $field) { if (isset($serializerMetadata['fields'][$field]['unserializeIgnore']) && $serializerMetadata['fields'][$field]['unserializeIgnore']) { continue; } $return[] = $field; } if (!$includeId) { unset($return[$metadata->getIdentifier()]); } return $return; }
/** * Insert one new record using the Entity class. * @param ObjectManager $manager * @param bool $generateId * @return EntityPopulator */ public function execute(ObjectManager $manager, $insertedEntities, $generateId = false) { $obj = $this->class->newInstance(); $this->fillColumns($obj, $insertedEntities); $this->callMethods($obj, $insertedEntities); if ($generateId) { $idsName = $this->class->getIdentifier(); foreach ($idsName as $idName) { $id = $this->generateId($obj, $idName, $manager); $this->class->reflFields[$idName]->setValue($obj, $id); } } $manager->persist($obj); return $obj; }
/** * @inheritdoc */ public function reverseTransform($value) { if (!is_array($value) || count($value) === 0) { return new ArrayCollection(); } if ($this->tags && $this->property) { $objects = []; foreach ($value as $val) { if (!is_numeric($value)) { $objects[] = $this->metadata->newInstance(); $this->propertyAccessor->setValue(end($objects), $this->property, $val); } $objects[] = $this->em->getRepository($this->class)->find($val); } return $objects; } return $this->em->getRepository($this->class)->findBy([$this->metadata->getIdentifier()[0] => $value]); }
/** * {@inheritdoc} */ public function readMetadata(ClassMetadata $doctrineMeta, array &$meta) { $reflectionClass = $doctrineMeta->getReflectionClass(); $copyingPolicy = null; if (isset($meta['clonable'])) { $copyingPolicy = $meta['clonable']['copyingPolicy']; } $clonableAnnotation = $this->reader->getClassAnnotation($reflectionClass, Clonable::ANNOTATION); if ($clonableAnnotation instanceof Clonable) { $copyingPolicy = $clonableAnnotation->copyingPolicy; } if (empty($copyingPolicy)) { return; } if (!isset($meta['clonable'])) { $meta['clonable'] = []; } $meta['clonable']['copyingPolicy'] = $copyingPolicy; $properties = $this->getPropertiesToCopy($reflectionClass, $copyingPolicy, $doctrineMeta->getIdentifier()); $meta['clonable']['properties'] = isset($meta['clonable']['properties']) ? array_merge($meta['clonable']['properties'], $properties) : $properties; }
/** * Checks if the method is a short identifier getter. * * What does this mean? For proxy objects the identifier is already known, * however accessing the getter for this identifier usually triggers the * lazy loading, leading to a query that may not be necessary if only the * ID is interesting for the userland code (for example in views that * generate links to the entity, but do not display anything else). * * @param \ReflectionMethod $method * @param \Doctrine\Common\Persistence\Mapping\ClassMetadata $class * * @return boolean */ private function isShortIdentifierGetter($method, ClassMetadata $class) { $identifier = lcfirst(substr($method->getName(), 3)); $startLine = $method->getStartLine(); $endLine = $method->getEndLine(); $cheapCheck = $method->getNumberOfParameters() == 0 && substr($method->getName(), 0, 3) == 'get' && in_array($identifier, $class->getIdentifier(), true) && $class->hasField($identifier) && $endLine - $startLine <= 4; if ($cheapCheck) { $code = file($method->getDeclaringClass()->getFileName()); $code = trim(implode(' ', array_slice($code, $startLine - 1, $endLine - $startLine + 1))); $pattern = sprintf(self::PATTERN_MATCH_ID_METHOD, $method->getName(), $identifier); if (preg_match($pattern, $code)) { return true; } } return false; }
/** * Extract the property annotations. * * @param \ReflectionProperty[] $reflProperties * @param ClassMetadata|\Revinate\SearchBundle\Lib\Search\Mapping\ClassMetadata $metadata * * @return ClassMetadata|\Revinate\SearchBundle\Lib\Search\Mapping\ClassMetadata */ private function extractPropertiesAnnotations(array $reflProperties, ClassMetadata $metadata) { foreach ($reflProperties as $reflProperty) { foreach ($this->reader->getPropertyAnnotations($reflProperty) as $annotation) { foreach ($this->entityFieldAnnotationClasses as $fieldAnnotationClass) { if ($annotation instanceof $fieldAnnotationClass) { if ($annotation instanceof $this->entityIdAnnotationClass) { $metadata->setIdentifier($reflProperty->name); } elseif ($annotation instanceof $this->entityTimeSeriesFieldAnnotationClass) { $metadata->setTimeSeriesField($reflProperty->name); } elseif ($annotation instanceof $this->entityParamAnnotationClass) { $metadata->addParameterMapping($reflProperty, $annotation); } elseif ($annotation instanceof $this->entityParentFieldAnnotationClass) { $metadata->setParentField($reflProperty->name); } elseif ($annotation instanceof $this->entityVersionFieldAnnotationClass) { /** @var Search\VersionField $annotation */ $metadata->setVersionField($reflProperty->name); $metadata->setVersionType($annotation->versionType); } else { $metadata->addFieldMapping($reflProperty, $annotation); } continue 2; } } } } $id = $metadata->getIdentifier(); if (empty($id)) { throw new \Exception(__METHOD__ . ': Id field must be defined!'); } if ($metadata->timeSeriesScale && !$metadata->getTimeSeriesField()) { throw new \Exception(__METHOD__ . ': TimeSeriesField must be defined for a time series index!'); } return $metadata; }