[moka同学笔记]yii2.0数据库操作以及分页
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数据库操作以及分页的更多相关文章
- [moka同学笔记]yii2.0查询数据库
一. [:id占位符]使用 $results = Test::findBySql($sql,array(':id'=>'1 or 1=1))->all() 二. [id=1] 选 ...
- [moka同学笔记]yii2.0缓存
1.控制器中CacheDemoController.php <?php /** * Created by PhpStorm. * User: moka同学 * Date: 2016/06/29 ...
- [moka同学笔记]Yii2.0 modal的使用
第一次使用,时候不明白什么原理,大概用了几次后,才模模糊糊搞清楚原来是怎么一回事,现在就把写过的代码,贴在下边. 1.在视图文件中, 第一步首先在index.php文件中 做了一个a链接的按钮 调用了 ...
- [moka同学笔记]Yii2.0验证码
1.Model中Code.php <?php /** * Created by PhpStorm. * User: moka同学 * Date: 2016/07/25 * Time: 10:48 ...
- [moka同学笔记]yii2.0表单的使用
1.创建model /biaodan.php <?php /** * Created by PhpStorm. * User: moka同学 * Date: 2016/08/05 * Tim ...
- [moka同学笔记]yii2.0小物件的简单使用(第二种方法)
1.在widgets/TestWidget.php中 <?php /** * Created by PhpStorm. * User: moka同学 * Date: 2016/08/05 * T ...
- [moka同学笔记]yii2.0小物件的简单使用(第一种方法)
这是第一种方法,还有另一种方法,其实都差不多. 1.在创建widgets\HelloWiget.php <?php /** * Created by PhpStorm. * User: Admi ...
- [moka同学笔记]yii2.0的下拉菜单与bootstrap下拉菜单
1.yii2下拉菜单 <li class="dropdown"><a href="#" class="dropdown-toggle ...
- [moka同学笔记]yii2.0 advanced高级版 安装配置 与 rbac (Ⅰ)
1.下载地址:http://www.yiichina.com/download,下载 Yii2 的高级应用程序模板 2.配置与安装 在服务器www目录下yii2test [下载下来更改advance ...
随机推荐
- javaBean List Map json(转)
1.依赖的包 根据:http://json-lib.sourceforge.net/ 可见json-lib使用需要依赖一下包: 下载地址:http://download.csdn.net/down ...
- 使用wireshark抓取wcf生成的soap消息
在使用wcf的时候想看下生成的soap的格式是怎样的,就想到了抓包. 平时用惯的抓包工具是需要破解,另外有时会不太好用. 于是就想起来用wireshark. 首先遇到几个问题: 1.wireshart ...
- 使用Aspose.Cells 根据模板生成excel里面的 line chart
目的: 1.根据模板里面的excel数据信息,动态创建line chart 2.linechart 的样式改为灰色 3.以流的形式写到客户端,不管客户端是否装excel,都可以导出到到客户端 4.使用 ...
- Binary Tree Postorder Traversal--leetcode难题讲解系列
https://leetcode.com/problems/binary-tree-postorder-traversal/ Given a binary tree, return the posto ...
- iOS8设置应用图标红点的权限问题
在ios8中,直接设置application badge value会出错 1 [[UIApplication sharedApplication] setApplicationIconBadgeNu ...
- python 集合set
python的set和其他语言类似, 是一个无序不重复元素集, 基本功能包括关系测试和消除重复元素. 集合对象还支持union(联合), intersection(交), difference(差)和 ...
- win平台,apache通过web访问svn
上个月用php写一个在线打包lua变成luac,碰到个权限问题.pysvn无法更新.网上搜了好半天都找不到解决方法.最好还是自己解决了. 解决方法也很简单,找到aphache服务,提权限就行了.lin ...
- Android showing LoadingView or EmptyView for Activity or Fragment
这几天利用一些时间,整理了一下,写了一个简单的工具类,用来快速的显示LoadingView或者EmptyView,以前都是定义一个layout,根布局为FrameLayout,再在layout布局文件 ...
- 将在本地创建的Git仓库push到Git@OSC
引用自:http://my.oschina.net/flan/blog/162189 在使用git 处理对android的修改的过程之中总结的.但不完善 Git push $ git push ori ...
- 【C语言】zz优先队列的实现
做一个题目时,看见解法中使用了优先队列,http://hawstein.com/posts/3.6.html . 颇为好奇,找资料学习了一下,顺便做个摘要. c++的用法: 转自:http://blo ...