1.model中models/article.php

 1 <?php
2
3 namespace app\models;
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 \yii\db\ActiveRecord
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

 <?php

 namespace app\controllers;

 use Yii;
use app\models\Article;
use yii\data\ActiveDataProvider;
use yii\db\Query;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\data\Pagination;
class ArticleController extends Controller
{ //public $layout="main";
public function actionIndex()
{
$article = Article::find();
$articleCount = clone $article; $pageSize = 5; $pages =new Pagination([
'totalCount'=>$articleCount->count(),
'pageSize'=>$pageSize
]) ; $models = $article->offset($pages->offset)
->limit($pages->limit)
->orderBy('id DESC')
->all(); return $this->render('index',[
'models'=>$models,
'pages'=>$pages
]);
}
}

3.视图中view/article/index.php

 <?php
/**
* Created by PhpStorm.
* User: moka同学
* Date: 2016/07/22
* Time: 11:13
*/
use yii\widgets\LinkPager;
use \yii\helpers\Url;
?>
<?php
foreach ($models as $model) {
?>
<div class="artcicle-list">
<h4 class="text-left h4">
<a href="<?=Url::toRoute(['article/view','id'=>$model->id]);?>" class="not-set"><?= $model->title ?></a>
</h4> <p class="text-right">
<small><?= $model->create_at ?></small>
</p>
<div class="col-l">
<?= isset($model->image) ? "<div class='face-image'><img src='$model->image' style='width: 120px;height: 120px;margin-right: 10px;vertical-align: text-top '></div>" : ""; ?>
<div class="article-content fl"><?=mb_substr($model->content,0,400,'utf-8').'……' ?><a href="<?=Url::toRoute(['article/view','id'=>$model->id]);?>">查看详情</a></div>
</div>
</div>
<?php } ?>
<div class="pagination-sm text-center">
<?= LinkPager::widget([
'pagination' => $pages,
'options' => [
'class' => 'pagination',
]
]) ?>
</div>

这个是很基础的model使用,如有不对,请联系我。QQ1727728211

[moka同学笔记]yii2.0数据库操作以及分页的更多相关文章

  1. [moka同学笔记]yii2.0查询数据库

      一. [:id占位符]使用 $results = Test::findBySql($sql,array(':id'=>'1 or 1=1))->all()   二. [id=1]  选 ...

  2. [moka同学笔记]yii2.0缓存

    1.控制器中CacheDemoController.php <?php /** * Created by PhpStorm. * User: moka同学 * Date: 2016/06/29 ...

  3. [moka同学笔记]Yii2.0 modal的使用

    第一次使用,时候不明白什么原理,大概用了几次后,才模模糊糊搞清楚原来是怎么一回事,现在就把写过的代码,贴在下边. 1.在视图文件中, 第一步首先在index.php文件中 做了一个a链接的按钮 调用了 ...

  4. [moka同学笔记]Yii2.0验证码

    1.Model中Code.php <?php /** * Created by PhpStorm. * User: moka同学 * Date: 2016/07/25 * Time: 10:48 ...

  5. [moka同学笔记]yii2.0表单的使用

    1.创建model   /biaodan.php <?php /** * Created by PhpStorm. * User: moka同学 * Date: 2016/08/05 * Tim ...

  6. [moka同学笔记]yii2.0小物件的简单使用(第二种方法)

    1.在widgets/TestWidget.php中 <?php /** * Created by PhpStorm. * User: moka同学 * Date: 2016/08/05 * T ...

  7. [moka同学笔记]yii2.0小物件的简单使用(第一种方法)

    这是第一种方法,还有另一种方法,其实都差不多. 1.在创建widgets\HelloWiget.php <?php /** * Created by PhpStorm. * User: Admi ...

  8. [moka同学笔记]yii2.0的下拉菜单与bootstrap下拉菜单

    1.yii2下拉菜单 <li class="dropdown"><a href="#" class="dropdown-toggle ...

  9. [moka同学笔记]yii2.0 advanced高级版 安装配置 与 rbac (Ⅰ)

    1.下载地址:http://www.yiichina.com/download,下载 Yii2 的高级应用程序模板 2.配置与安装 在服务器www目录下yii2test  [下载下来更改advance ...

随机推荐

  1. 解决企业In-House安装APP需HTTPS支持的问题(转载)

    同事写的一篇文章,感觉不错,转过来. 解决企业In-House安装APP需HTTPS支持的问题 问题背景: 能否通过应用服务器发布企业应用: 解决iOS7.1后,发布地址必须为HTTPS服务器. 写作 ...

  2. sql server trace 和 Profiler

    MS SQL Server Profiler概述:       MS SQL Server Profiler是SQL Trace的GUI接口,提供对SQL Server Database Engine ...

  3. 804 pretest 解题

    Answers with Explanations 1. c) s1 and s2 not equal s1 and s3 equal JVM sets a constant pool in whic ...

  4. [转]安卓开发startservice 和bindservice详解

    原文 作者:aikongmeng 来源:安卓中文网 博主暗表:搜到此文,终于为我解惑,bindService并不会真正启动service,不会调用onStartCommand!还需要再bind之前st ...

  5. hao.360.cn不停跳....

    最近单位里访问hao.360.cn经常会跳....无限循环,有时跳几十次后才会打开.... 但是,单位里走电信出口部分的电脑就没有问题...同样的电脑(移动出口)的用360浏览器.火狐也问题不大,关键 ...

  6. linux_脚本应用

    linux下三个有用的 Python 脚本 2010年4月29日   import os, sys  def pyc_clean(dir):      findcmd = 'find %s -name ...

  7. PHP实现简单的监控nginx日志文件功能

    此功能是为了实现,定时监控nginx生成的日志数据,并将新增的数据提交到一个接口(比如大数据的接口,让大数据来进行分析) define("MAX_SHOW", 8192*5); / ...

  8. C++的Trigraph

    ??=include <stdio.h> class HelloWolrd ??< public: void Trigraph() ??< printf("Hello ...

  9. linux2.6.24内核源代码分析(1)——扒一扒sk_buff

    最近研究了linux内核的网络子系统上的网络分组的接收与发送的流程,发现这个叫sk_buff的东西无处不在,内核利用了这个结构来管理分组,在各个层中传递这个结构,因此sk_buff可以说是linux内 ...

  10. Hadoop第1~2周练习—Hadoop1.X和2.X安装

        练习题目     Hadoop1.X安装 2.1    准备工作 2.1.1   硬软件环境 2.1.2   集群网络环境 2.1.3   安装使用工具 2.2  环境搭建 2.2.1   安 ...