function it_commit(\BX\DB\UnitOfWork\Repository $repo) { $repo->setLazy($this->entity, Argument::any())->willReturn(true); $this->setRepository($repo); $this->validate()->shouldBe(true); $this->commit()->shouldBe('2'); $return = ['ID' => '2', 'TEST' => 'TEST']; $this->db()->query('SELECT * FROM tbl_test WHERE ID = 2')->fetch()->shouldBe($return); }
/** * Delete news * * @param Repository $repo * @param NewsCategoryEntity $entity * @return boolean * @throws \RuntimeException */ public function delete(Repository $repo, NewsCategoryEntity $entity) { $repo->delete($this, $entity); if (!$repo->commit()) { $mess = print_r($repo->getErrorEntity()->getErrors()->all(), 1); throw new \RuntimeException("Error delete category news. Error: {$mess}."); } return true; }
/** * Delete all user group member by list * * @param Repository $repo * @param UserGroupMemberEntity[] $entities * @return boolean */ public function deleteAll(Repository $repo, $entities) { foreach ($entities as $entity) { $repo->delete($this, $entity); } if (!$repo->commit()) { $mess = print_r($repo->getErrorEntity()->getErrors()->all(), 1); throw new \RuntimeException("Error delete user group members. Error: {$mess}."); } return true; }
/** * Save in db * * @return false|integer */ public function commit() { $context = ['table' => $this->table->getDbTable(), 'fields' => $this->fields]; foreach ($this->fields as $field) { if ($field instanceof LazyValue) { $this->log('db.unitofwork.entity_add')->error('Find lazy value', $context); $this->entity->addError(false, $this->trans('db.unitofwork.find_lazy_value')); return false; } } $id = $this->db()->add($this->table->getDbTable(), $this->fields); if ($id > 0) { $this->id = $id; $this->fields[$this->table->getPkColumn()] = $id; if (!$this->repo->setLazy($this->entity, $id)) { return false; } return $id; } else { $error = print_r($this->db()->adaptor()->pdo()->errorInfo(), 1); $this->log('db.unitofwork.entity_add')->error('Error inset row. Error:' . $error); $this->entity->addError(false, $this->trans('db.unitofwork.add_unknow_error')); return false; } }
/** * Clear old counter * @param integer $day * @throws \RuntimeException * @return true */ public function clearOld($day = 30) { $time = $this->date()->convertTimeStamp(time() - $day * 3600 * 24); $filter = ['<' . CounterEntity::C_TIMESTAMP_X => $time]; $repo = new Repository('counter'); $counters = self::finder(CounterEntity::getClass())->filter($filter)->all(); foreach ($counters as $counter) { $repo->delete($this, $counter); } if (!$repo->commit()) { $mess = print_r($repo->getErrorEntity()->getErrors()->all(), 1); throw new \RuntimeException('Error delete old counter. Message: ' . $mess); } return true; }
/** * Clear old captcha * @param integer $day * @return boolean * @throws \RuntimeException */ public function clearOld($day) { $repository = new Repository('captcha'); $time = $this->date()->convertTimeStamp(time() - $day * 3600 * 24); $captches = static::finder(CaptchaEntity::getClass())->filter(['<' . CaptchaEntity::C_TIMESTAMP_X => $time])->all(); foreach ($captches as $captcha) { $repository->delete($this, $captcha); } if (!$repository->commit()) { $mess = print_r($repository->getErrorEntity()->getErrors()->all(), 1); throw new \RuntimeException('Error clear old captches. Error:' . $mess); } return true; }
/** * Create captcha * @return \BX\Captcha\Entity\CaptchaEntity * @throws \RuntimeException */ public function create() { $entity = new CaptchaEntity(); $repo = new \BX\DB\UnitOfWork\Repository('captcha'); $repo->add($this, $entity); if (!$repo->commit()) { $mess = print_r($repo->getErrorEntity()->getErrors()->all(), 1); throw new \RuntimeException("Error create captcha. Error: {$mess}."); } return $entity; }
/** * Down command * @return boolean */ public function down() { $this->found = true; $class = $this->package . '\\' . $this->service . '\\Migration'; if (!class_exists($class)) { throw new \LogicException("Class `{$class}` is not found"); } $instanse = new $class(); $trans = new Repository(); foreach ($this->getLastFunctions() as $func) { call_user_func_array([$instanse, $func->function], [false]); $trans->delete($this->table, $func); } if (!$trans->commit(false)) { throw new \RuntimeException('Delete migration error'); } }
/** * Delete news * * @param Repository $repo * @return NewsEntity $entity * @return boolean * @throws \RuntimeException */ public function delete(Repository $repo, NewsEntity $entity) { $repo->delete($this, $entity); $picture = $entity->picture; if (!$repo->commit()) { $mess = print_r($repo->getErrorEntity()->getErrors()->all(), 1); throw new \RuntimeException("Error delete news. Error: {$mess}."); } if ($picture !== null) { $picture->deleteFile(); } return true; }
/** * Delete user * * @param Repository $repo * @param UserEntity $entity * @return boolean * @throws \RuntimeException */ public function delete(Repository $repo, UserEntity $entity) { $repo->delete($this, $entity); if (!$repo->commit()) { throw new ValidateException($repo->getErrorEntity()->getErrors()); } return true; }
/** * Clear olds token * * @param integer $day * @return true * @throws \RuntimeException */ public function clearOld($day) { $time = $this->date()->convertTimeStamp(time() - $day * 3600 * 24); $filter = ['<' . AccessEntity::C_TIMESTAMP_X => $time]; $repo = new Repository($this->repository_name); $entities = self::finder(AccessEntity::getClass())->filter($filter)->all(); foreach ($entities as $entity) { $repo->delete($this, $entity); } if (!$repo->commit()) { $mess = print_r($repo->getErrorEntity()->getErrors()->all(), 1); throw new \RuntimeException("Error delete old access token. Error: {$mess}."); } return true; }