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. 算法 - 求两个自然数的最小公倍数(C++)

    //************************************************************************************************** ...

  2. 利用vba将excel中的图片链接直接转换为图片

    Sub test() Dim rg As Range, shp As Shape Rem --------------------------------------------------- Rem ...

  3. c#如何读取相机手机的拍摄时间

    /// 获中的照片拍摄日期 /// </summary> /// <param name="fileName">文件名</param> /// ...

  4. 开发者app 上传收集

    直接使用酷传  看各个市场目录 http://publish.coolchuan.com/myaccount/accounts 注册帐号 百度开发者 http://jingyan.baidu.com/ ...

  5. 比较下OceanBase的选举协议和Raft的选举协议的区别

    阿里技术大讲堂OceanBase专场中曾有专门一场讲座介绍OB自己实现的分布式选举算法:<分布式选举-破解数据库高可用性难题> 这里简单列一下这个选举算法和raft论文中提到的选举算法的区 ...

  6. 一步步教你搭建VS环境下用C#写WebDriver脚本

    一步步教你搭建VS环境下用C#写WebDriver脚本http://www.automationqa.com/forum.php?mod=viewthread&tid=3529&fro ...

  7. .net和java和谐相处之安卓客户端+.net asp.net mvc webapi 2

    作为没有花很多时间转java,把java当C#用的我,在做服务器端程序的时候,自然不想考虑java web,java需要学的框架太多了,看了一下Java Servlet,始终没有编码的冲动.经过几天的 ...

  8. 各种UserAgent的列表

    User Agent是浏览器用于 HTTP 请求的用户代理头的值.更换User Agent能更好的模拟出不同的系统和浏览器信息. Android Name User Agent Nexus 7 (Ta ...

  9. 使用Doxygen生成net帮助文档

    一. 什么是Doxygen? Doxygen 是一个程序的文件产生工具,可将程序中的特定批注转换成为说明文件.通常我们在写程序时,或多或少都会写上批注,但是对于其它人而言,要直接探索程序里的批注,与打 ...

  10. 转:C/C++内存管理详解 堆 栈

    http://chenqx.github.io/2014/09/25/Cpp-Memory-Management/ 内存管理是C++最令人切齿痛恨的问题,也是C++最有争议的问题,C++高手从中获得了 ...