-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;
}
I 검색이 실패하면 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
프로파일 러를 켜고 실행중인 SQL을 확인하십시오. – zombor
게시물에 실제로 질문이 포함 된 경우 더 좋을 것입니다 ... –