2012-03-27 4 views
-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; 
} 
+0

무엇이 문제입니까? 질문을 수정하여 지정하십시오 – illEatYourPuppies

+0

프로파일 러를 켜고 실행중인 SQL을 확인하십시오. – zombor

+0

게시물에 실제로 질문이 포함 된 경우 더 좋을 것입니다 ... –

답변

0

당신이 데이터베이스 사용의 특정 항목이 있는지 확인하기 위해 단지 필요한 경우 ->() 실행이 -()> 계수;

실제 게시물이 필요하므로 Database_Result 클래스의 count 메소드 (Countable 인터페이스 구현)를 사용할 수 있습니다.

$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; 

그런 try-catch 블록을 제거하십시오. 조회가 올 바르면 필요하지 않습니다.

관련 문제