50 public static function create($id =
null)
52 return new static(
$id);
84 foreach ($data as $key => $value)
86 if (!$entity->hasField($key))
96 foreach ($data as $key => $value)
98 if (!isset($previousData[$key]))
103 if ($previousData[$key] !== $data[$key])
111 return count($data) > 0;
124 protected function saveByEntity(MainEntityBase $entity, $id, array $data, $primary =
null)
127 $className = $entity->getDataClass();
129 $primary = $primary ?: $id;
133 if (array_key_exists(
'ID', $data))
137 $resultDb = $className::update($primary, $data);
141 $resultDb = $className::add($data);
142 $id = $resultDb->getId();
145 if(!$resultDb->isSuccess())
147 $this->errors->add($resultDb->getErrors());
161 protected function removeByEntity(MainEntityBase $entity, $primary)
164 $className = $entity->getDataClass();
165 $result = $className::delete($primary);
167 if(!$result->isSuccess())
169 $this->errors->add($result->getErrors());
190 abstract protected function saveData($id, array $data);
199 protected function copyData($id, array $data = array())
206 unset($loadedData[
'ID']);
207 $data = $data + $loadedData;
209 if (isset($data[
'FIELDS']))
211 foreach ($data[
'FIELDS'] as $index => $field)
213 if ($field[
'TYPE'] !==
'file')
218 if (empty($field[
'VALUE']))
223 $values = is_array($field[
'VALUE']) ? $field[
'VALUE'] : explode(
',', $field[
'VALUE']);
224 $field[
'VALUE'] = array();
225 foreach ($values as $fileId)
227 $copiedFileId = \CFile::copyFile($fileId);
233 $field[
'VALUE'][] = $copiedFileId;
235 $field[
'VALUE'] = implode(
',', $field[
'VALUE']);
236 $data[
'FIELDS'][$index] = $field;
240 return $this->
saveData(
null, $data);
252 $this->
setId((isset($data[
'ID']) && $data[
'ID']) ? $data[
'ID'] :
null);
275 if (!is_array($data))
343 public function set($key, $value)
345 $this->data[$key] = $value;
357 unset($this->data[$key]);
368 public function get($key, $defaultValue =
null)
370 return (isset($this->data[$key]) ? $this->data[$key] : $defaultValue);
412 return !$this->errors->isEmpty();
420 $this->errors->clear();
431 $this->errors->setError(
new Error($message, $code));
441 return $this->errors;
451 return $this->errors->toArray();
459 public function getErrorMessages()
462 foreach ($this->errors as $error)
465 $list[] = $error->getMessage();
492 $this->user = Security\User::current();
515 static $builder =
null;
516 if ($builder ===
null && static::getDataClass())
518 $dataClass = static::getDataClass();
520 $dataClass::getEntity(),
521 static::SEARCH_FIELD_NAME
static loadMessages($file)