/** * Delete an existing record * @param number $id * @throws \Exception * @return boolean */ public function delete($id) { $object = Category::findFirstById($id); if (!$object) { throw new \Exception('Object not found'); } if (false === $object->delete()) { foreach ($object->getMessages() as $message) { $error[] = (string) $message; } throw new \Exception(json_encode($error)); } return true; }
/** * Update an existing article * * @param number $id * @param array $input_data * @throws \Exception * @return unknown */ public function update($input_data) { $article = Article::findFirstById($input_data['id']); if (!$article) { throw new \Exception('Article not found', 404); } $data = $this->prepareData($input_data); $article->setArticleIsPublished($data['article_is_published']); $article->setArticleUpdatedAt(new \Phalcon\Db\RawValue('NOW()')); foreach ($data['translations'] as $lang => $translation) { $article->getTranslations()->filter(function ($t) use($lang, $translation) { if ($t->getArticleTranslationLang() == $lang) { $t->assign($translation); $t->update(); } }); } $results = ArticleCategoryArticle::findByArticleId($input_data['id']); if ($results) { $results->delete(); } if ($data['categories']) { $article->categories = Category::find(["id IN (" . $data['categories'] . ")"])->filter(function ($category) { return $category; }); } $results = ArticleHashtagArticle::findByArticleId($input_data['id']); if ($results) { $results->delete(); } if ($data['hashtags']) { $article->hashtags = Hashtag::find(["id IN (" . $data['hashtags'] . ")"])->filter(function ($hashtag) { return $hashtag; }); } $user = User::findFirstById((int) $data['article_user_id']); if (!$user) { throw new \Exception('User not found', 404); } $article->setArticleUserId($data['article_user_id']); return $this->save($article, 'update'); }