視圖模型顯示數(shù)據(jù)
視圖模型顯示數(shù)據(jù)
我們使用視圖模型來顯示列表的所屬欄目和所屬品牌
我們要把上圖中的3和1顯示他所屬的名稱,需要用的視圖模型
GoodsViewModel.class.php
model文件夾創(chuàng)建文件
<?php namespace Admin\Model; use Think\Model\ViewModel; class GoodsViewModel extends ViewModel { protected $viewFields = array( 'Goods'=>array('id','goods_name','sm_thumb','market_price','shop_price','onsale','cate_id','brand_id'), 'Cate'=>array('catename', '_on'=>'goods.cate_id=Cate.id','_type'=>'LEFT'), 'Brand'=>array('brand_name', '_on'=>'goods.brand_id=brand.id'), ); }
修改goods商品控制器
public function index(){ $goods = D('GoodsView'); $count = $goods->count(); $Page = new \Think\Page($count,25); $show = $Page->show(); $list = $goods->order('id desc')->limit($Page->firstRow.','.$Page->listRows)->select(); $this->assign('list',$list); $this->assign('page',$show); $this->display(); }
GoodsView是上面視圖模型的名稱,使用D方法加載。
列表顯示所屬欄目和所屬品牌更改為視圖模型所新定義的名稱
<td align="left"><a target="_brank" href="#">{$vo.catename}</a></td> <td align="left"><a target="_brank" href="#">{$vo.brand_name}</a></td>
名稱顯示成功