dphw5101 2012-03-27 19:28
浏览 37

Kohana 3.2 - 数据库搜索返回空对象

Try to make 'archive' for my blog. If the search run for unavaliable items the return is an empty object. Here is my code:

For example wrong input:

http://www.my-site.com/archive/2011/01/27 - in database no post with this date 2011-01-27

The controller action:

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 am try to throw 404 exception if the search fails. Here comes the model:

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;
}
  • 写回答

1条回答 默认 最新

  • dousendun8411 2012-04-03 19:19
    关注

    If you need just to check if there is a particular entry in the database use ->execute()->count();

    In your case you will need the actual posts, so you can use the count method of the Database_Result class (it implements the Countable interface).

    $posts = $posts_model->get_post_by_date($year . "-" . $month . "-" . $day);
    if ($posts->count()) {
        $this->template->content = View::factory('posts/default')
            ->bind('posts', $posts);
    } else
        throw new HTTP_Exception_404;
    

    And remove that try-catch block. You don't need it if your query is correct.

    评论

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法