博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
YII框架学习(二)
阅读量:7172 次
发布时间:2019-06-29

本文共 4643 字,大约阅读时间需要 15 分钟。

YII框架的增删改查

例:一个新闻表的增删改查:

(1)首先使用gii工具生成控制器和模型

(2)控制器

baseUrl ;die; //获取当前控制器路径 //echo Yii::app()->request->url;die; //方式一: //多个条件查要用and连接 //$n=News::model()->find("title=:title and slug=:slug",array(":title"=>'234',":slug"=>'234')); //var_dump($n->text);die; //方式二: $criteria = new CDbCriteria(); $criteria->order = 'id desc'; //计算总数 $count = News::model()->count($criteria); $pager = new CPagination($count); //设置页大小 $pager->pageSize = 4; $pager->applyLimit($criteria); $news = News::model()->findAll($criteria); //获取当前页 $num = Yii::app()->request->getParam('page')?Yii::app()->request->getParam('page'):1; $data['news'] = $news; $data['pages'] = $pager; //分配当前页起始编号 $data['num'] = ($num-1)*$pager->pageSize; //将变量分配到模板 $this->render('index',$data); } //新增数据 public function actionAdd(){ //新增时要new $newsModel = new News(); //设置初始值,避免报错 $info->id = ''; $info->title = ''; $info->text = ''; if(!empty($_POST)){ $newsModel->title = $_POST['title']; $newsModel->text = $_POST['text']; $newsModel->slug = 'aa'; //判断更新还是新增 if(!empty($_POST['id'])){ //更新 $res = News::model()->updateByPk($_POST['id'], array('title'=>$_POST['title'],'text'=>$_POST['text'])); if($res){ $this->redirect(array('index')); }else{ $info = News::model()->findByPk($_POST['id']); $data['info'] = $info; } }else{ //新增 // $res = News::model()->newsAdd($_POST['title'],$_POST['text'], '123test'); if($newsModel->save()){ $this->redirect(array('index')); } } }else{ //获取GET/POST传过来的参数 $id = Yii::app()->request->getParam('id'); if(!empty($id)){ $info = News::model()->findByPk($id); $data['info'] = $info; } } $this->render('add', $data); } //删除 public function actionDel(){ $id = Yii::app()->request->getParam('id'); $res= News::model()->findByPk($id)->delete(); // 假设有一个帖子,其 ID 为 10 $this->redirect(array('index')); }

(3)模型(gii生成的)

128), // The following rule is used by search(). // Please remove those attributes that should not be searched. array('id, title, slug, text', 'safe', 'on'=>'search'), ); } /** * @return array relational rules. */ public function relations() { // NOTE: you may need to adjust the relation name and the related // class name for the relations automatically generated below. return array( ); } /** * @return array customized attribute labels (name=>label) */ public function attributeLabels() { return array( 'id' => 'ID', 'title' => 'Title', 'slug' => 'Slug', 'text' => 'Text', ); } /** * Retrieves a list of models based on the current search/filter conditions. * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions. */ public function search() { // Warning: Please modify the following code to remove attributes that // should not be searched. $criteria=new CDbCriteria; $criteria->compare('id',$this->id); $criteria->compare('title',$this->title,true); $criteria->compare('slug',$this->slug,true); $criteria->compare('text',$this->text,true); return new CActiveDataProvider($this, array( 'criteria'=>$criteria, )); }}

(4)视图:

index.php

编号 标题 内容 操作
title?>
text?>
编辑 删除
widget('CLinkPager',array( 'header'=>'', 'firstPageLabel' => '首页', 'lastPageLabel' => '末页', 'prevPageLabel' => '上一页', 'nextPageLabel' => '下一页', 'pages' => $pages, 'maxButtonCount'=>13 ) ); ?>

 

add.php

添加

标题:
内容:

 

转载于:https://www.cnblogs.com/zrp2013/p/3651685.html

你可能感兴趣的文章
(九)企业部分之Varnish
查看>>
在sublime中,css设置snippet后缀源要写.less才生效(内附50个css代码段链接)
查看>>
2015.05.26 工作任务与心得
查看>>
JAVA中关于Synchronized和Volatile的测试
查看>>
Linux程序包管理学习
查看>>
EMC:数据保护进入以服务交付的时代
查看>>
LAMP下phpwind的搭建
查看>>
Mybatis CRUD标签使用 模板
查看>>
Linux文件查找之find命令
查看>>
ntp服务搭建
查看>>
单例 double check
查看>>
Mac下的shell命令总结
查看>>
[翻译] Facebook 的 iOS 内存泄漏监测自动化实践
查看>>
基于Nginx实现10万+并发,你应该做的Linux内核优化
查看>>
生产环境中使用Docker Swarm的一些建议
查看>>
java.io.Serializable
查看>>
PostgreSQL在Windows下的数据迁移
查看>>
数据库连接池(JDBC、DBCP、C3P0三种实现)
查看>>
Android零基础入门第9节:Android应用实战,不懂代码也可以开发
查看>>
【警惕】大量未修复WebLogic WSAT组件RCE漏洞的主机被挖矿程序攻击
查看>>