-1
嘗試爲我的博客製作'歸檔'。如果搜索運行不可變項目,則返回是一個空對象。這裏是我的代碼:Kohana 3.2 - 數據庫搜索返回空對象
例如輸入錯誤:
http://www.my-site.com/archive/2011/01/27 - 在數據庫中沒有使用後此日期2011-01-27
控制器動作:
public function action_archive() {
$posts_model = new Model_Posts();
// Év pl.: 2012
if($year = $this->request->param("year")) {
// Hónap pl.: 2012-03
if($month = $this->request->param("month")) {
// Nap pl.: 2012-03-27
if($day = $this->request->param("day")) {
if ($posts = $posts_model->get_post_by_date($year . "-" . $month . "-" . $day)) {
$this->template->content = View::factory('posts/default')
->bind('posts', $posts);
} else
throw new HTTP_Exception_404;
} else {
if($posts = $posts_model->get_post_by_date($year . "-" . $month)) {
$this->template->content = View::factory('posts/default')
->bind('posts', $posts);
} else
throw new HTTP_Exception_404;
}
} else {
if($posts = $posts_model->get_post_by_date($year)) {
$this->template->content = View::factory('posts/default')
->bind('posts', $posts);
} else
throw new HTTP_Exception_404;
}
} else
// Nem található archívum
throw new HTTP_Exception_404;
return false;
}
我如果搜索失敗,我會嘗試拋出404異常。這裏談到的模型:
public function get_post_by_date($date) {
try {
return DB::select()
->from("posts")
->where("date", "like", "$date%")
->and_where("publish", "=", "1")
->as_object()
->execute();
} catch (Database_Exception $e) {
Kohana::$log->add(Log::ERROR, Database_Exception::text($e));
}
return false;
}
什麼問題?編輯您的問題請指定 – illEatYourPuppies 2012-03-27 19:51:33
嘗試打開探查器並查看它運行的SQL是什麼。 – zombor 2012-03-27 19:52:13
這將是更好,如果發佈實際上包含一個問題... – 2012-03-27 19:53:06