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. php 生成 Json

    php 生成 Json 部分 <?php $arr_result = array(); //返回值 $arr_result['result'] = '0'; $arr_result['calle ...

  2. 一致性哈希算法 - consistent hashing

    1 基本场景比如你有 N 个 cache 服务器(后面简称 cache ),那么如何将一个对象 object 映射到 N 个 cache 上呢,你很可能会采用类似下面的通用方法计算 object 的 ...

  3. JDBC性能分析与优化

    JDBC性能分析与优化V1.0http://www.docin.com/p-758600080.html

  4. cocos2d ios 环境搭建

    一.下载cocos2d-x http://cocos2d-x.org/projects/cocos2d-x/wiki/Download cocos2d-x-2.1.4.zip @ June.18, 2 ...

  5. xml和xsd架构文档相关知识

    1.使用架构(XSD)验证XML文件 2.使用自动生成工具: 工具目录:C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4. ...

  6. Failure is not fatal, but failure to change might be.

    Failure is not fatal, but failure to change might be. 失败不是致命的,但无法改变却可能是致命的.

  7. 金蝶EAS常用表

    select * from T_SCM_BillType where FName_L2 like '%委外%'   --单据类型表,查业务单据对应的表  102--销售出库单  330--应收单 se ...

  8. 转载:Linux kernel rt_mutex的背景和简介

    From: http://blog.chinaunix.net/uid-23146151-id-3074045.html 最近在看linux rtmutex部分的实现过程,就像他注释中写的一样,还是比 ...

  9. css省略号布局实例截图

    过多文字li标签出现使用css省略号样式截图 使用text-overflow样式让显示不完内容通过css实现省略号排版

  10. joomla3.1安装不通过Magic Quotes GPC解决方法

    测试安装下joomla 3.1稳定版,但是不能成功,Magic Quotes GPC始终显示 否红色,这样就安装不了了! 要解决这个很简单,开启Magic Quotes GPC就行了,于是找到php. ...