10use Bitrix\Im\Model\EO_Chat;
11use Bitrix\Im\Model\EO_Message;
12use Bitrix\Im\Model\EO_MessageParam;
21trait ActiveRecordImplementation
24 protected $dataObject;
36 protected static function mirrorDataEntityFields(): array
47 if ($this->dataObject ===
null)
53 $dataClass = static::getDataClass();
54 $entityObjectClass = $dataClass::getObjectClass();
55 $this->dataObject =
new $entityObjectClass;
58 return $this->dataObject;
67 $this->dataObject = $dataObject;
74 public function load($source):
Result
78 if (is_numeric($source))
80 $source = (int)$source;
85 $dataClass = static::getDataClass();
86 $dataObject = $dataClass::getByPrimary($source)->fetchObject();
90 && $dataObject->hasId()
101 elseif ($source instanceof EntityObject)
106 elseif (is_array($source))
115 if ($result->isSuccess() && $this->getPrimaryId())
120 $this instanceof RegistryEntry
124 $this->
getRegistry()[$this->getPrimaryId()] = $this;
136 foreach (static::mirrorDataEntityFields() as $field)
139 !isset($field[
'primary'])
140 && !isset($field[
'alias'])
141 && isset($field[
'field'], $field[
'default'])
142 && !isset($this->{$field[
'field']})
143 && ($default = $field[
'default'])
144 && is_string($default)
145 && is_callable([$this, $default])
148 $this->{$field[
'field']} = $this->$default();
163 foreach (static::mirrorDataEntityFields() as $offset => $field)
165 if (isset($field[
'alias']) || !isset($field[
'field']))
169 if ($this->getDataEntity()->has($offset))
172 isset($field[
'loadFilter'])
173 && ($loadFilter = $field[
'loadFilter'])
174 && is_string($loadFilter)
175 && is_callable([$this, $loadFilter])
178 $this->{$field[
'field']} = $this->$loadFilter($this->getDataEntity()->
get($offset));
182 $this->{$field[
'field']} = $this->getDataEntity()->get($offset);
187 if ($result->isSuccess() && $this->getPrimaryId())
202 $fieldsToFill = $source;
206 [$ormFields, $fieldsToFill] = $this->separateFieldsOrmAndOther($source);
207 $result = $this->
initByDataEntity(static::getDataClass()::getObjectClass()::wakeUp($ormFields));
208 if (!$result->isSuccess())
214 if (!empty($fieldsToFill))
216 $this->
fill($fieldsToFill);
219 if ($result->isSuccess() && $this->getPrimaryId())
229 $fields = static::mirrorDataEntityFields();
231 foreach ($source as $key => $value)
233 if (!isset($fields[$key]))
238 $field = isset($fields[$key][
'alias']) ? $fields[$fields[$key][
'alias']] : $fields[$key];
240 if (isset($field[
'primary']) && $field[
'primary'])
249 protected function separateFieldsOrmAndOther(array $source): array
251 $fields = static::mirrorDataEntityFields();
253 $entity = static::getDataClass()::getEntity();
257 foreach ($source as $key => $value)
259 if (!isset($fields[$key]))
264 $fieldName = $fields[$key][
'alias'] ?? $key;
266 if ($entity->hasField($fieldName) && !($entity->getField($fieldName) instanceof
Relation))
274 if (is_string($value) && !is_numeric($value))
278 if (is_numeric($value))
283 $ormFields[$fieldName] = $value;
287 $otherFields[$key] = $value;
291 return [$ormFields, $otherFields];
294 public function prepareFields(): Result
296 $result =
new Result;
298 foreach (static::mirrorDataEntityFields() as $offset => $field)
300 if (isset($field[
'primary']) || isset($field[
'alias']) || (isset($field[
'skipSave']) && $field[
'skipSave'] ===
true))
306 isset($field[
'beforeSave'])
307 && ($beforeSave = $field[
'beforeSave'])
308 && is_string($beforeSave)
309 && is_callable([$this, $beforeSave])
313 $check = $this->$beforeSave();
314 if (!$check->isSuccess())
316 $result->addErrors($check->getErrors());
321 if (isset($field[
'field'], $this->{$field[
'field']}))
324 isset($field[
'saveFilter'])
325 && ($saveFilter = $field[
'saveFilter'])
326 && is_string($saveFilter)
327 && is_callable([$this, $saveFilter])
330 $this->getDataEntity()->set($offset, $this->$saveFilter($this->{$field[
'field']}));
334 $this->getDataEntity()->set($offset, $this->{$field[
'field']});
349 $result = $this->prepareFields();
350 if (!$result->isSuccess())
359 $saveResult = $this->getDataEntity()->save();
360 if ($saveResult->isSuccess())
364 $this->setPrimaryId((
int)$saveResult->getId());
371 $this->
getRegistry()[$this->getPrimaryId()] = $this;
378 $result->addErrors($saveResult->getErrors());
396 if ($this->getDataEntity()->hasId())
398 $deleteResult = $this->getDataEntity()->delete();
399 if (!$deleteResult->isSuccess())
401 return $result->
addErrors($deleteResult->getErrors());
410 unset($this->
getRegistry()[$this->getPrimaryId()]);
432 $this->markedDrop =
false;
453 $this->markedDrop =
true;
468 foreach ($fieldsToFill as $fieldName)
470 if ($this->getDataEntity()->entity->hasField($fieldName))
472 $this->getDataEntity()->unset($fieldName);
475 $this->getDataEntity()->fill($fieldsToFill);
487 public function fill(array $source): self
489 $fields = static::mirrorDataEntityFields();
491 foreach ($fields as $offset => $field)
493 if (isset($source[$offset]))
495 if (isset($field[
'primary']))
499 if (isset($field[
'alias']))
501 $field = $fields[$field[
'alias']];
505 && ($setter = $field[
'set'])
506 && is_string($setter)
507 && is_callable([$this, $setter])
510 $this->$setter($source[$offset]);
512 elseif (isset($field[
'field']))
514 $this->{$field[
'field']} = $source[$offset];
529 $fields = static::mirrorDataEntityFields();
531 foreach ($fields as $offset => $field)
533 if (isset($field[
'alias']))
539 && ($getter = $field[
'get'])
540 && is_string($getter)
541 && is_callable([$this, $getter])
544 $value = $this->$getter();
548 $value = $this->{$field[
'field']};
551 if (is_object($value))
553 if (method_exists($value,
'toArray'))
555 $value = $value->toArray();
565 $result[$offset] = $value;
static createFromTimestamp($timestamp)
static tryParse($timeString, $format=null)
fillActual(array $fieldsToFill)
setDataEntity(EntityObject $dataObject)
markChanged(?bool $state=null)
initByDataEntity(EntityObject $dataObject)
hasPrimary(array $source)
initByArray(array $source)