1.model中models/article.php
1 <?php
2
3 namespace appmodels;
4
5 use Yii;
6
7 /**
8 * This is the model class for table "yii2_article".
9 *
10 * @property string $id
11 * @property integer $category_id
12 * @property string $title
13 * @property string $image
14 * @property string $content
15 * @property string $create_at
16 * @property string $updata_at
17 */
18 class Article extends yiidbActiveRecord
19 {
20 /**
21 * @inheritdoc
22 */
23 public static function tableName()
24 {
25 return 'yii2_article';
26 }
27
28 /**
29 * @inheritdoc
30 */
31 public function rules()
32 {
33 return [
34 [['category_id'], 'integer'],
35 [['desc','content'], 'string'],
36 [['create_at', 'updata_at'], 'safe'],
37 [['title'], 'string', 'max' => 50],
38 [['image'], 'string', 'max' => 100]
39 ];
40 }
41
42 /**
43 * @inheritdoc
44 */
45 public function attributeLabels()
46 {
47 return [
48 'id' => 'ID',
49 'category_id' => '栏目',
50 'title' => '标题',
51 'desc' => '描述',
52 'image' => '封面图片',
53 'content' => '内容',
54 'create_at' => '创建日期',
55 'updata_at' => '修改日期',
56 ];
57 }
58
59 //根据文章查询栏目的信息,hasOne()因为一个文章只属于一个栏目,一比一的方法
60 public function getArticleCategory(){
61 return $this->hasOne(ArticleCategory::className(),['id'=>'category_id']);
62 }
63 }
2.控制器中ArticleController.php
1 <?php 2 3 namespace appcontrollers; 4 5 use Yii; 6 use appmodelsArticle; 7 use yiidataActiveDataProvider; 8 use yiidbQuery; 9 use yiiwebController; 10 use yiiwebNotFoundHttpException; 11 use yiidataPagination; 12 class ArticleController extends Controller 13 { //public $layout="main"; 14 public function actionIndex() 15 { 16 $article = Article::find(); 17 $articleCount = clone $article; 18 19 $pageSize = 5; 20 21 $pages =new Pagination([ 22 'totalCount'=>$articleCount->count(), 23 'pageSize'=>$pageSize 24 ]) ; 25 26 $models = $article->offset($pages->offset) 27 ->limit($pages->limit) 28 ->orderBy('id DESC') 29 ->all(); 30 31 return $this->render('index',[ 32 'models'=>$models, 33 'pages'=>$pages 34 ]); 35 } 36 }
3.视图中view/article/index.php
1 <?php 2 /** 3 * Created by PhpStorm. 4 * User: moka同学 5 * Date: 2016/07/22 6 * Time: 11:13 7 */ 8 use yiiwidgetsLinkPager; 9 use yiihelpersUrl; 10 ?> 11 <?php 12 foreach ($models as $model) { 13 ?> 14 <div class="artcicle-list"> 15 <h4 class="text-left h4"> 16 <a href="<?=Url::toRoute(['article/view','id'=>$model->id]);?>" class="not-set"><?= $model->title ?></a> 17 </h4> 18 19 <p class="text-right"> 20 <small><?= $model->create_at ?></small> 21 </p> 22 <div class="col-l"> 23 <?= isset($model->image) ? "<div class='face-image'><img src='$model->image' style=' 120px;height: 120px;margin-right: 10px;vertical-align: text-top '></div>" : ""; ?> 24 <div class="article-content fl"><?=mb_substr($model->content,0,400,'utf-8').'……' ?><a href="<?=Url::toRoute(['article/view','id'=>$model->id]);?>">查看详情</a></div> 25 </div> 26 </div> 27 <?php } ?> 28 <div class="pagination-sm text-center"> 29 <?= LinkPager::widget([ 30 'pagination' => $pages, 31 'options' => [ 32 'class' => 'pagination', 33 ] 34 ]) ?> 35 </div>
这个是很基础的model使用,如有不对,请联系我。QQ1727728211