Issue
I've used CakePHP 2.0 for quite a while, and I'm trying to migrate to 3.0. Most of it is fine, but I'm confused by how to retrieve data from a table and show it in a view.
Let's say my database contains menus and menuitems. Each Menu can have many Menuitems (each Menuitem record has field menu_id).
So, in CakePHP 2.0 I would have had the following:
Model:
Menu hasMany Menuitem
Controller:
$this->set->recursive = 1;
$menu = $this->Menu->findById($menuid);
View:
foreach($menu['Menuitem'] as $item) {
... display item ...
}
In Cake 3.0 I'm trying to replicate this. So, I have:
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'));
How do I now display this in the view?
If I try:
foreach($menu['Menuitem'] as $item)
then I get the error "Undefined index: Menuitem".
Then I tried $menu.menuitems
and $menu->menuitems
but those didn't work either. Where am I going wrong?
Edit
If I do debug($menulist)
I get the following:
(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'
}
]
Solution
On the basis of what ('menulist') variable you have set , your foreach should look like:
foreach($menulist->Menuitems as $item)
{
// your logic here
}
Just debug your set variable and see how the data are coming , sometimes that might come differently depending on your query.
Answered By - Manohar Khadka
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.