View model displays data

View model displays data

We use the view model to display the columns and brands of the list

QQ截图20170629165051.png

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>

QQ截图20170630134810.png

The name is displayed successfully

Continuing Learning
||
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<!DOCTYPE html>
<html><head>
<meta charset="utf-8">
<title>PHP</title>
<meta name="description" content="Dashboard">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!--Basic Styles-->
<link href="__PUBLIC__/style/bootstrap.css" rel="stylesheet">
<link href="__PUBLIC__/style/font-awesome.css" rel="stylesheet">
<link href="__PUBLIC__/style/weather-icons.css" rel="stylesheet">
<!--Beyond styles-->
<link id="beyond-link" href="__PUBLIC__/style/beyond.css" rel="stylesheet" type="text/css">
<link href="__PUBLIC__/style/demo.css" rel="stylesheet">
<link href="__PUBLIC__/style/typicons.css" rel="stylesheet">
<link href="__PUBLIC__/style/animate.css" rel="stylesheet">
</head>
<body>
<!-- -->
<include file="Common/header" />
<!-- / -->
<div class="main-container container-fluid">
<div class="page-container">
<!-- Page Sidebar -->
<include file="Common/left" />
<!-- /Page Sidebar -->
<!-- Page Content -->
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
submitReset Code
图片放大关闭