我在这里遇到了一些问题。我对DB中的每个产品都有category_id。我也有类别和他们的ID在DB中的类别表。现在我需要一起放入视野。我做了添加,编辑和删除操作,还显示了操作,其中的类别显示在产品描述的其余部分。但是现在我遇到了索引操作的问题。Zend Framework:使用控制器方法调用模型方法
在节目我这样做:
public function getProductTable()
{
if (!$this->productTable) {
$sm = $this->getServiceLocator();
$this->productTable = $sm->get('Product\Model\ProductTable');
}
return $this->productTable;
}
public function getCategoryTable() {
if(!$this->categoryTable){
$this->categoryTable = $this->getServiceLocator()
->get('Product\Model\CategoryTable');
}
return $this->categoryTable;
}
public function showAction()
{
$id = (int) $this->params()->fromRoute('id', 0);
if (!$id) {
return $this->redirect()->toRoute('product', array(
'action' => 'add'
));
}
try {
$product = $this->getProductTable()->getProduct($id);
$category = $this->getCategoryTable()->getCategory($product->category_id);
}
catch (\Exception $ex) {
return $this->redirect()->toRoute('product', array(
'action' => 'index'
));
}
这很容易,在的showAction期间,因为我从DB得到一个结果,所以我确切地知道什么CATEGORY_ID产品。
但是,在index.phtml中,我将从数据库获取所有产品,并需要通过foreach来迭代它们。这就是我需要得到调用
$this->getCategoryTable()->getCategory($id);
由于地方,这是用SM使用模型方法控制方法,我应该如何使用这在我index.phtml视图为每一个产品得到确切的类别名称?
非常感谢酥:)我考虑做这种方式,但不知何故,我卡住了,你真的帮助我。 :) –