我正在尝试从另一个控制器检索数据,以使用元素在PagesController的视图中显示它。我有一张桌子
service_categories(id, service_category);
我的ServiceCategoriesController看起来像这样
public function category() {
$serviceCategories = $this->paginate();
if ($this->request->is('requested')) {
return $servicesCategories;
} else {
$this->set('serviceCategories', $servicesCategories);
}
}
我的category.ctp元素看起来像这样
<?php
$serviceCategories = $this->set('serviceCategories/category');
foreach ($serviceCategories as $serviceCategory):
echo $serviceCategory['ServiceCategory']['service_category'];
endforeach;
但是当我在foreach循环之前创建警报时,我似乎得到了“$ serviceCategories”的未定义值。请协助!我错过了什么?
答案 0 :(得分:0)
我在打开大脑后重写了答案:
在元素替换中:
$serviceCategories = $this->set('serviceCategories/category');
与
$serviceCategories = $this->requestAction('service_categories/category');