Versuchen Sie, "Archiv" für meinen Blog zu machen. Wenn die Suche nach unavailable Elementen ausgeführt wird, ist die Rückgabe ein leeres Objekt. Hier ist mein Code:Kohana 3.2 - Datenbanksuche gibt leeres Objekt zurück
Zum Beispiel falscher Eingabe:
http://www.my-site.com/archive/2011/01/27 - in der Datenbank keinen Beitrag mit diesem Datum 2011-01-27
Die Controller Aktion:
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 Ich versuche, 404 Ausnahme auszulösen, wenn die Suche fehlschlägt. Hier kommt das Modell:
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;
}
Was ist das Problem? Bearbeiten Sie Ihre Frage bitte, um anzugeben – illEatYourPuppies
Versuchen Sie, den Profiler einzuschalten und zu sehen, was das SQL ist, dass es läuft. – zombor
Es wäre noch netter, wenn Post tatsächlich eine Frage enthielt ... –