View model displays data
View model displays data
We use the view model to display the columns and brands of the list
We need to display the names of 3 and 1 in the above picture, and the view model we need to use
GoodsViewModel.class.php
model folder creation file
<?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'), ); }
Modify goods product controller
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 is the name of the above view model and is loaded using the D method.
The column and brand displayed in the list are changed to the newly defined name of the view model
<td align="left"><a target="_brank" href="#">{$vo.catename}</a></td> <td align="left"><a target="_brank" href="#">{$vo.brand_name}</a></td>
The name is displayed successfully