Php Kohana foreach ORM对象的多个循环提前终止

Php Kohana foreach ORM对象的多个循环提前终止,php,foreach,kohana-3.3,Php,Foreach,Kohana 3.3,我正在使用Kohana3.3并尝试将页面输出到菜单和所有子菜单。子菜单与页面存储在同一个表中,称为页面。页面和子菜单之间的唯一区别是标志。这是页面的表结构和我创建的示例页面 | id | title | layout | content | is_menu | parent | position | | 1 | Home | home.php | Lipsum... | 0 | 0 | 1 |

我正在使用Kohana3.3并尝试将页面输出到菜单和所有子菜单。子菜单与页面存储在同一个表中,称为页面。页面和子菜单之间的唯一区别是标志。这是页面的表结构和我创建的示例页面

|  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对象加载到一个数组中,按父对象排序,然后按位置排序。在查询获取它们时,我对上面的表进行了排序。这是我的PHP方法,用于生成HTML菜单,然后将ORM页面数组传递到其中:

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;
}
因此,在第二次调用子菜单ID 3的menu方法之后,foreach循环完成ID 3,但不会继续到下一页。我不明白为什么?我以前也做过这样的foreach循环,这是我第一次在Kohana中做,虽然我认为这与Kohana有关,但我不理解

我已经创建了一个,展示了代码的工作原理,并且它一定是在Kohana中完成的。这是来自eval.in的所有回波的结果:

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

正如您可以看到的,与Kohana不同,在第3页完成处理后,它会按预期继续到第2页。科哈纳为什么不这样做?

我已经找到了问题和解决方案。Kohana通过引用处理ORM对象。我不知道为什么这会阻止我的foreach循环完成,但确实如此

解决方案是在ORM对象中循环一次,将每个页面的详细信息存储在stdClass数组中。这样,它们就不是引用,所以当我将新数组传递给我的方法时,它就起作用了


如果其他人能给出更完整的答案,解释为什么引用对象数组会出现此问题,我将不胜感激。

我不知道它为什么会出现此问题,但我确实认为此解决方案会有所帮助:

更改查询,使find_all成为executenull,true;它将返回一个对象,应该可以正常工作


如果这不是重点,那么变化是这只是Kohana中的一个bug。

模型没有执行方法,所以我不能这样做。如果我没有弄错的话,这是Kohana::ORM的标准函数。我尝试它时,它说它不存在。execute是由DB类扩展的Kohana_DB方法,而不是由ORM扩展的Kohana_ORM方法。。。对于ORM,你已经找到并找到了一切
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