我在這裏遇到了一些問題。我對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視圖爲每一個產品得到確切的類別名稱?
非常感謝酥:)我考慮做這種方式,但不知何故,我卡住了,你真的幫助我。 :) –