Exemplo n.º 1
0
 /**
  * Сохраняет запись
  * @return bool
  */
 public function save()
 {
     if (!$this->validate() || !$this->riseEvent('beforeSave')) {
         return false;
     }
     $class = \bxar\helpers\HlEntity::compile($this->getEntity());
     $arLoad = array();
     foreach ($this->getAttributes() as $key => $attr) {
         $arLoad[strtoupper($key)] = $attr->getValueToDb();
     }
     if (!$this->isNew()) {
         $id = $this->getAttribute('id')->getValue();
         $res = $class::update($id, $arLoad);
         if (!$res->isSuccess()) {
             $errors = implode(', ', $res->getErrorMessages());
             throw new Exception($errors);
         }
         $this->riseEvent('afterSave');
     } else {
         $res = $class::add($arLoad);
         if (!$res->isSuccess()) {
             $errors = implode(', ', $res->getErrorMessages());
             throw new Exception($errors);
         }
         $this->getAttribute('id')->setValue($res->getId());
         $this->riseEvent('afterSave');
     }
     return true;
 }
Exemplo n.º 2
0
 /**
  * Запрашиваем данные из базы
  * @return array
  */
 protected function getList()
 {
     $class = \bxar\helpers\HlEntity::compile($this->getEntity());
     if (!$class) {
         throw new Exception('Entity does not set.');
     }
     $return = array();
     $arQuery = [];
     //сортировка
     $order = $this->getOrder();
     if ($order) {
         $arQuery['order'] = $order;
     }
     //условия для поиска
     $filter = $this->getFilter();
     if ($filter) {
         $arQuery['filter'] = $filter;
     }
     //ограничение количества
     $limit = (int) $this->getLimit();
     if ($limit) {
         $arQuery['limit'] = $limit;
     }
     //смещение
     $offset = (int) $this->getOffset();
     if ($offset) {
         $arQuery['offset'] = $offset;
     }
     //runtime
     $runtime = $this->getRuntime();
     if ($runtime) {
         foreach ($runtime as $rt) {
             $arQuery['runtime'][] = new \Bitrix\Main\Entity\ExpressionField($rt[0], $rt[1]);
         }
     }
     return $class::getList($arQuery)->fetchAll();
 }