Example #1
0
 public function addAction()
 {
     if ($this->request->isPost() == true) {
         $ans = [];
         try {
             $title = $this->request->getPost("title");
             $date = $this->request->getPost("date");
             $body = $this->request->getPost("body");
             $section = $this->request->getPost("section");
             $existArticle = Article::find(array("conditions" => "title=?1", "bind" => array(1 => $title)));
             if (count($existArticle) == 0) {
                 $article = new Article();
                 $article->title = $title;
                 $article->date = $date;
                 $article->body = ${$body};
                 $article->section = $section;
                 if ($article->save()) {
                     $ans['ret'] = $article->id;
                     echo json_encode($ans);
                 } else {
                     foreach ($article->getMessages() as $message) {
                         throw new BaseException($message, 100);
                     }
                 }
             } else {
                 $ans['ret'] = -1;
                 $ans['error'] = 201;
                 echo json_encode($ans);
                 throw new BaseException('同名文章已存在', 201);
             }
         } catch (BaseException $e) {
         }
     }
 }