2014-12-11 7 views
0

나는 Kohana 3.3을 사용 중이며 페이지를 메뉴 및 모든 하위 메뉴로 출력하려고합니다. 하위 메뉴는 page라고하는 동일한 테이블에 저장됩니다. 페이지와 하위 메뉴 간의 유일한 차이점은 플래그입니다. 이것은 페이지의 테이블 구조와이 작업을 위해 작성한 예제 페이지입니다. ORM 부모와 다음 위치에 의해 주문, 배열로 객체로foreach ORM 개체의 Kohana 다중 루프는 일찍 종료됩니다.

| id | title | layout | content | is_menu | parent | position | 
| 1 | Home | home.php | Lipsum... |  0  | 0  |  1  | 
| 3 | Menu1 | none  |    |  1  | 0  |  2  | 
| 2 | Expl1 | view.php | Lipsum... |  0  | 0  |  3  | 
| 5 | MPge2 | view.php | Lipsum... |  0  | 3  |  1  | 
| 4 | MPge1 | view.php | Lipsum... |  0  | 3  |  2  | 

나는 모든 페이지를로드합니다. 위의 표는 쿼리에서 가져 오므로 주문했습니다. 그리고 이것은 내가 다음에 ORM 페이지의 배열을 전달하는 HTML 메뉴를 생성하는 내 PHP 방법입니다 :

class Controller_Page extends Controller_Table { 

/** 
* Ourput the menu for editing purposes. Include add new page buttons. 
* 
* @return body 
*/ 
public function action_acp_menu() 
{ 
    $view = View::factory('acp/layouts/pages/menu') 
     ->bind('menu_pages', $menu_pages); 
    $pages = ORM::factory('Page') 
     ->order_by('parent') 
     ->order_by('position') 
     ->find_all(); 
    $menu_pages = $this->menu($pages); 
    $this->response->body($view->render()); 
} 

/** 
* Output the pages in a menu format, with optional add more buttons if 
* we're in the ACP. This returns a string of LIs without a wrapping UL. 
* 
* @param ORM  $pages ORM object of pages. 
* @param integer $parent Output all children of this parent. 
* @return string 
*/ 
private function menu($pages, $parent = 0) 
{ 
    $html = ''; 
    $is_acp = (strpos(Request::initial()->uri(), 'acp') !== FALSE); 
    echo "<br>Testing pages against parent: $parent<br>"; 
    foreach ($pages as $page) 
    { 
     echo "Page: ".$page->id."; Parent: ".$page->parent."<br>"; 
     if ($page->parent == $parent) 
     { 
      echo "Page belongs to the parent.<br>"; 
      if ($page->is_menu) 
      { 
       echo "Page ".$page->id." is a menu. Loading children.<br>"; 
       $children = $this->menu($pages, $page->id); 
       if ($children OR $is_acp) 
       { 
        echo "Children found (or we're in the ACP). Adding pages to the menu.<br>"; 
        $html .= '<li class="dropdown" data-id="'.$page->id.'">'.($is_acp ? ' 
         <span class="handle">::</span>' : '').' 
         <a href="#" class="dropdown-toggle" data-toggle="dropdown"> 
          '.$page->title.' 
          <b class="caret"></b> 
         </a> 
         <ul class="dropdown-menu'.($is_acp ? ' sortable-child' : '').'" data-id="'.$page->id.'"> 
          '.$children.' 
         </ul> 
        </li>'; 
       } 
      } 
      else 
      { 
       echo "Page ".$page->id." is not a menu. Outputting normal.<br>"; 
       $html .= '<li data-id="'.$page->id.'">'.($is_acp ? ' 
        <span class="handle">::</span>' : '').' 
        <a href="/acp/pages/edit'.$page->id.'">'.$page->title.'</a> 
       </li>'; 
      } 
     } 
     echo "Finished processing page ".$page->id."<br>"; 
    } 
    echo "Finished looping all pages<br>"; 
    if ($is_acp) 
    { 
     $html .= '<li class="add-page"> 
      <a href="/acp/pages/create?parent='.$page->id.'">+ Add page</a> 
     </li>'; 
    } 
    echo "Finished testing against parent: $parent<br>"; 
    return $html; 
} 

당신은 내가 시도하고 무슨 일이 일어나고 있는지 알아 내기 위해 에코의에 잔뜩 넣었습니다 볼 수 있듯이 . 이것은 모든 에코의의 출력이다

Testing pages against parent: 0 
Page: 1; Parent: 0 
Page belongs to the parent. 
Page 1 is not a menu. Outputting normal. 
Finished processing page 1 
Page: 3; Parent: 0 
Page belongs to the parent. 
Page 3 is a menu. Loading children. 

Testing pages against parent: 3 
Page: 1; Parent: 0 
Finished processing page 1 
Page: 3; Parent: 0 
Finished processing page 3 
Page: 2; Parent: 0 
Finished processing page 2 
Page: 5; Parent: 3 
Page belongs to the parent. 
Page 5 is not a menu. Outputting normal. 
Finished processing page 5 
Page: 4; Parent: 3 
Page belongs to the parent. 
Page 4 is not a menu. Outputting normal. 
Finished processing page 4 
Finished looping all pages 
Finished testing against parent: 3 
Children found (or we're in the ACP). Adding pages to the menu. 
Finished processing page 3 
Finished looping all pages 
Finished testing against parent: 0 

그래서 후 서브 메뉴 (ID 3) foreach 루프는 ID 3 완료되지만, 다음에 계속되지 않을 수있는 메뉴 방식으로 두 번째 전화 페이지. 왜 그런지 모르겠다 고요? 나는 과거에 foreach 루프를 해왔다. Kohana에서이 작업을 수행 한 것은 처음이다. Kohana와 관련하여 내가 이해하지 못하는 것 같아.

나는 코드 작업을 보여주는 eval.in of this을 생성했으며이 작업은 Kohana에서 수행해야합니다. 당신이 처리 페이지 3 개 완료 후, Kohana 달리 볼 수 있듯이

Testing pages against parent: 0 
Page: 1; Parent: 0 
Page belongs to the parent. 
Page 1 is not a menu. Outputting normal. 
Finished processing page 1 
Page: 3; Parent: 0 
Page belongs to the parent. 
Page 3 is a menu. Loading children. 

Testing pages against parent: 3 
Page: 1; Parent: 0 
Finished processing page 1 
Page: 3; Parent: 0 
Finished processing page 3 
Page: 2; Parent: 0 
Finished processing page 2 
Page: 5; Parent: 3 
Page belongs to the parent. 
Page 5 is not a menu. Outputting normal. 
Finished processing page 5 
Page: 4; Parent: 3 
Page belongs to the parent. 
Page 4 is not a menu. Outputting normal. 
Finished processing page 4 
Finished looping all pages 
Finished testing against parent: 3 
Children found (or we're in the ACP). Adding pages to the menu.<br> 
Finished processing page 3 
Page: 2; Parent: 0 
Page belongs to the parent. 
Page 2 is not a menu. Outputting normal. 
Finished processing page 2 
Page: 5; Parent: 3 
Finished processing page 5 
Page: 4; Parent: 3 
Finished processing page 4 
Finished looping all pages 
Finished testing against parent: 0 

가 예상대로 2 페이지에 계속 :이 eval.in에서 모든 에코의의 결과입니다. 왜 Kohana는 이것을하지 않습니까?

답변

0

문제점 및 해결책을 찾았습니다. Kohana는 ORM 객체를 참조로 처리합니다. 왜 내 foreach 루프가 완료되는 것을 멈출 지 모르지만 그럴 수 있습니다.

해결 방법은 ORM 개체를 한 번 반복하여 각 페이지의 세부 정보를 stdClass의 배열에 저장하는 것입니다. 그렇게해서 그들은 참조가 아니었고 새로운 배열을 내 메서드에 전달할 때 그것은 효과가있었습니다.

다른 사람이 설명하는보다 완전한 답변을 게시 할 수있는 경우 참조 된 개체의 배열이 문제를 가지고 왜 나는 그것을 감사하겠습니다.

0

는이 작업을 수행하는 이유는 모르겠지만, 나는이 솔루션이 도움이 될 것 같아 :

, 당신의 조회를 변경 (null의 사실)을 실행 find_all 만들; 그것은 객체를 반환하고 그냥 작동해야합니다.

이것이 중요하지 않은 경우 변경 사항은 단지 Kohana의 버그 일뿐입니다.

+0

모델에'execute' 메소드가 없으므로이를 수행 할 수 없습니다. – Styphon

+0

Kohana :: ORM의 표준 기능으로 착각하지는 않습니다. – Chilion

+0

글쎄, 나는 그것을 시도했을 때 그것이 존재하지 않는다고 말했다. 또한 [api에 나열되지 않았습니다] (http://kohanaframework.org/3.3/guide-api/ORM). – Styphon