如何在CakePHP 3.0中显示检索到的数据?

时间:2016-10-31 20:16:19

标签: cakephp cakephp-3.0

我已经使用CakePHP 2.0很长一段时间了,我试图迁移到3.0。大部分都很好,但我真的很困惑如何从表中检索数据并在视图中显示它。

我们说我的数据库包含菜单和菜单项。每个菜单都可以有许多菜单项(每个菜单项记录都有字段menu_id)。

所以,在CakePHP 2.0中我会得到以下内容:

Model:
Menu hasMany Menuitem

Controller:
$this->set->recursive = 1;
$menu = $this->Menu->findById($menuid);

View:
foreach($menu['Menuitem'] as $item) {
 ... display item ...
}

在Cake 3.0中,我试图复制这个。所以,我有:

In Model/Table/MenusTable.php:
    public function initialize(array $config)
    {
        $this->hasMany('Menuitems', [
                        'sort' => ['Menuitems.order' => 'ASC']
        ]);
    }

In Controller:
$menus= TableRegistry::get('Menus');
$query = $menus->find('all')
                    ->contain(['Menuitems');
$menulist= $query->toArray();
$this->set(compact('menulist'));

我现在如何在视图中显示它?

如果我尝试:

foreach($menu['Menuitem'] as $item)

然后我收到错误"未定义的索引:Menuitem"。

然后我尝试了$ menu.menuitems和$ menu-> menuitems,但那些也没有用。我哪里错了?谢谢!

编辑:抱歉几天消失了。如果我做调试($ menulist);我得到以下内容:

(int) 0 => object(Cake\ORM\Entity) {
    'id' => (int) 1,
    'name' => 'Main',
    'menuitems' => [
        (int) 0 => object(Cake\ORM\Entity) {

            'id' => (int) 1,
            'subsite_id' => (int) 1,
            'page_id' => (int) 1,
            'parentmenuitem_id' => null,
            'nav_order' => (int) 1,
            'nav_width' => null,
            'children' => [],
            '[new]' => false,
            '[accessible]' => [
                '*' => true
            ],
            '[dirty]' => [],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[invalid]' => [],
            '[repository]' => 'Menuitems'

        },
        (int) 1 => object(Cake\ORM\Entity) {

            'id' => (int) 2,
            'subsite_id' => (int) 1,
            'page_id' => (int) 2,
            'parentmenuitem_id' => null,
            'nav_order' => (int) 2,
            'nav_width' => null,
            'children' => [
                (int) 0 => object(Cake\ORM\Entity) {

                    'id' => (int) 9,
                    'subsite_id' => (int) 1,
                    'page_id' => (int) 9,
                    'parentmenuitem_id' => (int) 2,
                    'nav_order' => (int) 1,
                    'nav_width' => null,
                    '[new]' => false,
                    '[accessible]' => [
                        '*' => true
                    ],
                    '[dirty]' => [],
                    '[original]' => [],
                    '[virtual]' => [],
                    '[errors]' => [],
                    '[invalid]' => [],
                    '[repository]' => 'Children'

                },
                (int) 1 => object(Cake\ORM\Entity) {

                    'id' => (int) 10,
                    'subsite_id' => (int) 1,
                    'page_id' => (int) 13,
                    'parentmenuitem_id' => (int) 2,
                    'nav_order' => (int) 2,
                    'nav_width' => null,
                    '[new]' => false,
                    '[accessible]' => [
                        '*' => true
                    ],
                    '[dirty]' => [],
                    '[original]' => [],
                    '[virtual]' => [],
                    '[errors]' => [],
                    '[invalid]' => [],
                    '[repository]' => 'Children'

                },
                (int) 2 => object(Cake\ORM\Entity) {

                    'id' => (int) 11,
                    'subsite_id' => (int) 1,
                    'page_id' => (int) 14,
                    'parentmenuitem_id' => (int) 2,
                    'nav_order' => (int) 3,
                    'nav_width' => null,
                    '[new]' => false,
                    '[accessible]' => [
                        '*' => true
                    ],
                    '[dirty]' => [],
                    '[original]' => [],
                    '[virtual]' => [],
                    '[errors]' => [],
                    '[invalid]' => [],
                    '[repository]' => 'Children'

                }
            ],
            '[new]' => false,
            '[accessible]' => [
                '*' => true
            ],
            '[dirty]' => [],
            '[original]' => [],
            '[virtual]' => [],
            '[errors]' => [],
            '[invalid]' => [],
            '[repository]' => 'Menuitems'

        },
    ],
    '[new]' => false,
    '[accessible]' => [
        '*' => true
    ],
    '[dirty]' => [],
    '[original]' => [],
    '[virtual]' => [],
    '[errors]' => [],
    '[invalid]' => [],
    '[repository]' => 'Subsites'

}

2 个答案:

答案 0 :(得分:1)

根据您设置的(' menulist')变量,您的foreach应如下所示:

foreach($menulist->Menuitems as $item)
{
  // your logic here
}

只需调试您的set变量并查看数据的来源,有时根据您的查询可能会有所不同。

答案 1 :(得分:1)

您的返回数据是什么样的?您应该使用debug($menulist)检查变量,或者查看debugkit中的set视图变量。

在CakePHP 3中,包含的模型应该在您的数据中显示为小写的蛇形项目(不是CakePHP 2中的模型别名)和hasMany关联的复数形式,所以我希望你的案例是这样的事情: -

foreach ($menulist['menuitems'] as $item):
    // Output your menu item
endforeach;