75 foreach ($this->hiddenObjectFields as $fieldName)
77 unset($row[$fieldName]);
85 return $this->result->getFields();
90 return $this->result->getSelectedRowsCount();
95 return $this->result->fetchRowInternal();
103 final public function fetchObject()
111 $this->initializeFetchObject();
114 $row = $this->result->fetch();
128 $basePrimaryValues = [];
130 foreach ($this->primaryAliases as $primaryName => $primaryAlias)
133 $primaryField = $this->query->getEntity()->getField($primaryName);
134 $primaryValue = $primaryField->cast($row[$primaryAlias]);
136 $basePrimaryValues[$primaryName] = $primaryValue;
140 $baseAddToIM =
false;
141 $objectClass = $this->objectClass;
144 $object = $this->identityMap->get($objectClass, $basePrimaryValues);
148 $object =
new $objectClass(
false);
151 $object->sysChangeState(State::ACTUAL);
158 $relEntityCache = [];
161 foreach ($this->query->getSelectChains() as $selectChain)
167 $currentDefinitionParts = [];
168 $currentDefinition =
null;
171 $iterableElements = array_slice($selectChain->getAllElements(), 1);
174 foreach ($iterableElements as $element)
176 if ($currentObject ===
null)
182 $field = $element->getValue();
184 if (!($field instanceof Field))
192 $currentDefinitionParts[] = $field->getName();
193 $currentDefinition = join(
'.', $currentDefinitionParts);
196 $isRuntimeField = !empty($this->query->getRuntimeChains()[$currentDefinition]);
198 if ($field instanceof IReadable)
201 if ($currentObject !==
$object)
207 $value = $field->cast($row[$selectChain->getAlias()]);
211 ? $currentObject->sysSetRuntime($field->getName(), $value)
212 : $currentObject->sysSetActual($field->getName(), $value);
219 if (array_key_exists($currentDefinition, $relEntityCache))
221 $currentObject = $relEntityCache[$currentDefinition];
226 $remoteEntity = $field->getRefEntity();
230 $remotePrimary = $remoteEntity->getPrimaryArray();
231 $remoteObjectValues = [];
232 $remotePrimaryValues = [];
234 foreach ($this->selectChainsMap[$currentDefinition] as $remoteChain)
237 $remoteField = $remoteChain->getLastElement()->getValue();
238 $remoteValue = $row[$remoteChain->getAlias()];
240 $remoteObjectValues[$remoteField->getName()] = $remoteValue;
243 foreach ($remotePrimary as $primaryName)
245 if (!array_key_exists($primaryName, $remoteObjectValues))
247 throw new SystemException(sprintf(
248 'Primary of %s was not found in database result', $remoteEntity->getDataClass()
252 $remotePrimaryValues[$primaryName] = $remoteObjectValues[$primaryName];
256 if ($field instanceof Reference)
259 $remoteObject = $this->composeRemoteObject($remoteEntity, $remotePrimaryValues, $remoteObjectValues);
263 ? $currentObject->sysSetRuntime($field->getName(), $remoteObject)
264 : $currentObject->sysSetActual($field->getName(), $remoteObject);
266 elseif ($field instanceof OneToMany || $field instanceof ManyToMany)
271 if (empty($currentObject->sysGetRuntime($field->getName())))
275 $collection = $remoteEntity->createCollection();
276 $currentObject->sysSetRuntime($field->getName(), $collection);
280 $collection = $currentObject->sysGetRuntime($field->getName());
285 if (empty($currentObject->sysGetValue($field->getName())))
289 $collection = $remoteEntity->createCollection();
292 if ($this->query->getLimit() ===
null)
295 $noRelationInFilter =
true;
297 foreach ($this->query->getFilterChains() as $chain)
299 if (str_starts_with($chain->getDefinition(), $currentDefinition))
301 $noRelationInFilter =
false;
306 if ($noRelationInFilter)
309 $collection->sysSetFilled();
313 $currentObject->sysSetActual($field->getName(), $collection);
317 $collection = $currentObject->sysGetValue($field->getName());
322 if (current($remotePrimaryValues) ===
null || !$collection->hasByPrimary($remotePrimaryValues))
325 $remoteObject = $this->composeRemoteObject($remoteEntity, $remotePrimaryValues, $remoteObjectValues);
328 if ($remoteObject !==
null)
330 $collection->sysAddActual($remoteObject);
335 $remoteObject = $collection->getByPrimary($remotePrimaryValues);
338 foreach ($remoteObjectValues as $fieldName => $objectValue)
340 if (!$remoteObject->sysHasValue($fieldName))
342 $field = $remoteEntity->getField($fieldName);
343 $castValue = $field->cast($objectValue);
345 $remoteObject->sysSetActual($fieldName, $castValue);
352 throw new SystemException(
'Unknown chain element value while fetching object');
356 $currentObject = $remoteObject;
359 $relEntityCache[$currentDefinition] = $remoteObject;
367 $this->identityMap->put(
$object);
377 final public function fetchCollection()
380 $this->initializeFetchObject(
true);
383 $collection = $this->query->getEntity()->createCollection();
385 while (
$object = $this->fetchObject())
387 $collection->sysAddActual(
$object);
400 protected function initializeFetchObject($asCollection =
false)
402 if (empty($this->objectInitPassed))
405 if (!empty($this->query->getGroupChains()))
407 throw new SystemException(
408 'Result of query with aggregation could not be fetched as an object'
413 if (empty($this->identityMap))
416 $this->identityMap =
new IdentityMap;
419 $this->objectClass = $this->query->getEntity()->getObjectClass();
427 $this->objectInitPassed =
true;
430 if (!$asCollection && $this->query->hasBackReference())
433 $collection = $this->fetchCollection();
438 $this->result =
new ArrayResult($collection->getAll());
443 if ($originalResult->getCount())
445 $this->result->setCount($originalResult->getCount());
448 catch (\Bitrix\Main\ObjectPropertyException $e) {}
458 foreach ($this->query->getSelectChains() as $selectChain)
460 $this->selectChainsMap[$selectChain->getDefinition(-1)][] = $selectChain;
469 $primaryNames = $this->query->getEntity()->getPrimaryArray();
471 foreach ($this->query->getSelectChains() as $selectChain)
473 $field = $selectChain->getLastElement()->getValue();
476 if ($field->getEntity()->getDataClass() === $this->query->getEntity()->getDataClass()
477 && in_array($field->getName(), $primaryNames))
479 $this->primaryAliases[$field->getName()] = $selectChain->getAlias();
481 if (
count($this->primaryAliases) ==
count($primaryNames))
488 if (
count($this->primaryAliases) !=
count($primaryNames))
491 'Primary of %s was not found in database result', $this->query->getEntity()->getDataClass()
501 foreach ($this->query->getSelectChains() as $selectChain)
503 $alias = $selectChain->getAlias();
505 if (!isset($this->result->converters[$alias]))
507 $this->result->converters[$alias] = [
508 $this->result->getFields()[$alias],
524 protected function composeRemoteObject(
$entity, $primaryValues, $objectValues)
527 if (current($primaryValues) ===
null)
534 $objectClass =
$entity->getObjectClass();
535 $remoteObject = $this->identityMap->get($objectClass, $primaryValues);
540 if (empty($remoteObject))
543 $remoteObject =
new $objectClass(
false);
546 $remoteObject->sysChangeState(State::ACTUAL);
553 foreach ($objectValues as $fieldName => $objectValue)
556 $field =
$entity->getField($fieldName);
557 $castValue = $field->cast($objectValue);
559 $remoteObject->sysSetActual($fieldName, $castValue);
565 $this->identityMap->put($remoteObject);
568 return $remoteObject;
580 $this->identityMap =
$map;
590 return $this->identityMap;
596 return $this->result->getResource();
601 $this->result->setReplacedAliases($replacedAliases);
606 $this->result->addReplacedAliases($replacedAliases);
611 $this->result->setSerializedFields($serializedFields);
616 $this->result->addFetchDataModifier($fetchDataModifier);
621 return $this->result->fetchRaw();
626 $row = $this->result->fetch($converter);
628 if ($row && !empty($this->hiddenObjectFields))
638 if (empty($this->hiddenObjectFields))
640 return $this->result->fetchAll($converter);
644 $data = $this->result->fetchAll($converter);
646 foreach (
$data as &$row)
658 return $this->result->getTrackerQuery();
663 return $this->result->getConverters();
668 $this->result->setConverters($converters);
673 $this->result->setCount(
$n);
678 return $this->result->getCount();
683 return $this->result->getIterator();
if( $daysToExpire >=0 &&$daysToExpire< 60 elseif)( $daysToExpire< 0)