Yii常用执行SQL方法
======================================================
======================================================
//执行SQL:createCommand
query(); //select
queryAll();
queryRow();
queryScalar();
execute(); //delete insert update //举例
$sql = "SELECT LEFT(region_code,2) as provinceid,region_name";
$sql .= " FROM `dim_luna_region`";
$sql .= " GROUP BY provinceid ORDER BY region_code asc";
$provinces = Yii::app()->db->createCommand($sql)->queryAll();
//举例
Yii::app()->db->createCommand()
->select('name, started_date, finished_date')
->from('customer c')
->leftJoin('accounting a', 'c.id=a.customerid')
->rightJoin('customer_employee ce', 'c.id=ce.customerid')
->join('app_info_list il', 'dl.AppId = il.Id')
->where('ce.employeeid=:id', array(':id'=>2))
->group('AppId')
->order('value desc')
->limit(10)
->queryRow();
//举例
$command = Yii::app()->db->createCommand();
$command->select('count(Id) as countApp, sum(Up) as totalUp');
$command->from('app_info_list');
$command->where('CommitUserId = :CommitUserId', array(':CommitUserId' => $memberID));
$result = $command->queryRow();
//举例???
$userId = Yii::app()->db->createCommand($selectSql)->queryScalar();
//举例???
Yii::app()->db->createCommand($updateSql)->execute(); //执行SQL:CDbCriteria
//举例
$criteria = new CDbCriteria;
$criteria->alias = 'a';
$criteria->select = 'name, started_date, finished_date';
$criteria->join = 'RIGHT JOIN customer_employee ON customer.id=customer_employee.customerid ';
$criteria->join .= 'LEFT JOIN accounting ON customer.id=accounting.customerid';
$criteria->group = ' GROUP BY a.PushId';
$criteria->condition = 'customer_employee.employeeid=:id';
$criteria->params = array(':id'=>2);
$customers = Customers::model()->find($criteria); //执行SQL:model
find();
findAll();
findByPk();
findAllByAttributes();
count();
delete();
deleteAll(); //举例
$news = AppDownloadLog::model()->find('Hash = :hash', array(':hash' => $hash));
if (! $news instanceof AppDownloadLog) {
throw new THttpException('文章ID有误');
}
//举例
$models = AppInfoList::model()->findAll();
//举例
$yesterdayApps = AppInfoList::model()->findAll('CommitTime > "' . $yesterday . '" and CommitTime < "' . $today . '" and Status = 0');
//举例
$models_user = User::model()->findAllByAttributes(array('Status' => '-1'));
//举例
AppInfoList::model()->findByPk($id);
//举例
User::model()->count( "Status = 0 and CreateTime < '$yesterday' and LastLoginTime>='$yesterday'" );
//举例
$commentModel = AppPushListReviews::model()->findAll(
array(
'select' => array('Id', 'Sort', 'Up'), //可选,默认全部
'distinct' => true, //可选
'condition' => 'Status=0 and Used=0 and PushId=:PushId and Content !=""',
'params' => array(':PushId'=>$pushId),
'order' => 'Id desc', //'order' => new CDbExpression('RAND()'),
'limit' => 1
)
);
//举例
AppPushListDetail::model()->find('PushId=' . $row)->delete();
//举例
$deleteChildrenReplyNum = AppReviews::model()->deleteAll('Pid=:pid', array(':pid' => $reply->Id));
//扩展举例 published
$product = CoinExchangeProduct::model()->published()->findByPk($productID);
public function scopes()
{
return array(
'published' => array(
'condition' => 'status = 0'
)
);
}
//扩展举例 together
AppReviews::model()->with('author_icon')->together()->findAll(
array(
'select'=> array('Id', 'Pid', 'Content', 'UpdateTime', 'AuthorId', 'ToAuthorId'),
'order' => 't.Pid asc, t.UpdateTime desc',
'condition' => 'AppId = :AppId',
'params' => array(':AppId' => $appID)
)
); adp代码学习
======================================================
======================================================
多语言。加载messages/[module]
Yii::t($this->getModule()->ID, $this->ID . '_' . $field) 权限控制
components/controller.php function filters(){}
components/WebUser.php $this->perm;登录的时候存入的 权限设置数据表
module_adm Yii框架设置
======================================================
======================================================
//打开gii工具
后台打开 protected/config/main.php 搜索gii
访问gii http://localhost/shop/index.php?r=模块名字(gii) //创建模块成功配置
位置:protectd/config/main.php
'modules'=>array(
'admin' // 模块名称
), //默认控制器
前台默认控制器:SiteController.php
后台默认控制器:DefaultController.php //controller位置
protected/components //定义常量位置
protected/assets/default
引入已经定义好的系统常量 index.php
require_once(dirname(__FILE__).'/protected/config/constant.php'); //数据库
数据库配置:protected/config/database.php
注意:php.ini 开启扩展
extension=php_pdo_mysql.dll
extension=php_mysql.dll
检测是否连接上数据库:var_dump(Yii::app()->db); framework/web/CWebApplication.php
framework/base/CApplication.php
famework/YiiBase.php
framework/db/CDbConnection.php //全部代码
yii有10000行,全部在framework/yiilite.php 有体现 //表单小物件
$form = $this -> beginWidget('CActiveForm'); //form submit
注册:
给模型收集表单信息
foreach($_POST['User'] as $_k => $_v){
$user_model -> $_k = $_v;
} //上边的foreach,在yii框架里边有优化,使用模型属性attributes来进行优化
//attributes 属性已经把foreach集成好了,我们可以直接使用
$user_model -> attributes = $_POST['User']; input radio:(yiilist.php)
<?php echo $form->radioButtonList($user_model,'user_sex',$sex,array('separator'=>'&nbsp;')); ?>
input select:
<?php echo $form -> dropDownList($user_model,'user_xueli',$xueli); ?>
input checkbox:
<?php echo $form -> checkBoxList($user_model,'user_hobby',$hobby,array('separator'=>'&nbsp;')); ?>
表单显示错误信息:
<?php echo $form->textField($user_model,'username',array('class'=>'inputBg','id'=>'User_username')); ?>
<!--表单验证失败显示错误信息-->
<?php echo $form ->error($user_model,'username'); ?> //验证:
framework/validators/cValidator.php
举例:
array('username','required','message'=>'用户名必填'),
//用户名不能重复(与数据库比较)
array('username', 'unique', 'message'=>'用户名已经占用'),
array('password','required','message'=>'密码必填'),
//验证确认密码password2 要与密码的信息一致
array('password2','compare','compareAttribute'=>'password','message'=>'两次密码必须一致'),
//邮箱默认不能为空
array('user_email','email','allowEmpty'=>false, 'message'=>'邮箱格式不正确'),
//验证qq号码(都是数字组成,5到12位之间,开始为非0信息,使用正则表达式验证)
array('user_qq','match','pattern'=>'/^[1-9]\d{4,11}$/','message'=>'qq格式不正确'),
//验证手机号码(都是数字,13开始,一共有11位)
array('user_tel','match','pattern'=>'/^1[3,4,5,6,7,8]{1}\d{9}$/','message'=>'手机号码格式不正确'),
//验证学历(信息在2、3、4、5之间则表示有选择,否则没有),1正则;2范围限制
//范围限制
array('user_xueli','in','range'=>array(2,3,4,5),'message'=>'学历必须选择'),
//验证爱好:必选两项以上(自定义方法对爱好进行验证)
array('user_hobby','check_hobby'),
//为没有具体验证规则的属性,设置安全的验证规则,否则attributes不给接收信息
array('user_sex,user_introduce','safe'), Yii调试
======================================================
======================================================
//让mysql抛出异常信息(js里面也是可以看到的)
new CDbExpression($sql); //网站底部显示日志信息
位置:protectd/config/main.php
搜索:CWebLogRoute
array(
'class'=>'CWebLogRoute',
), Yii常用内置方法:
======================================================
======================================================
//获取当前Yii版本
Yii::getVersion(); //获取当前域名
Yii::app()->request->hostInfo //在控制器中获取控制器名:
$this->id;
$this->ID;
$name = $this->getId();
//在控制器beforeAction()回调函数中获取动作名
$name = $action->id;
//在控制器中获取modules名:
$this->getModule()->ID
//控制器中获取表名
$model->table;
//在视图中获取控制器名:
$name = Yii::app()->controller->id;
//在其他地方获取动作名:
$name = $this->getAction()->getId();
//在模型中获取
$this->getModule()->id
$this->Module->getId() Yii控制器
======================================================
======================================================
//获取字段属性
$model_new_creative = new LunaCreative();
$attrs = $model_new_creative->attributeNames(); //验证网址
if(!Func::validateIsUseful($model->website)){
throw new THttpException('网址不可用!');
} Yii定时任务
======================================================
======================================================
准备阶段:
要定时执行的PHP程序存放位置:trunk/protected/commands/***.php
执行阶段:
打开cmd,进入到相应目录,敲入命令yiic,即可看到所定制的任务计划
例如:D:\wamp\www\appgrub\trunk\protected>yiic
执行对应的文件
例如:D:\wamp\www\appgrub\trunk\protected>yiic report
此时已经默认执行了ReportCommand.php中的actionIndex()方法
如果要执行控制里面的其他方法actionshow()方法
例如:D:\wamp\www\appgrub\trunk\protected>yiic report show
如果要执行控制里面的其他方法actionshow($p1,$p2)方法
例如:D:\wamp\www\appgrub\trunk\protected>yiic report show --p1=*** --p2=***
执行成功 Yii Session
======================================================
======================================================
设置:Yii::app()->session['var']='value';
使用:Yii::app()->session['var'];
移除: unset(Yii::app()->session['var']); 最后,当用户退出登录(logout),你需要消除痕迹,可使用:
Yii::app()->session->clear() 移去所有session变量,然后,调用
Yii::app()->session->destroy() 移去存储在服务器端的数据。

Yii 学习笔记的更多相关文章

  1. yii学习笔记

    学而不思则罔,思而不学则殆,适度的总结有利于学习效果的提升. 以前就是埋头看书很少动手所以学习效果不好. 学习yii的原因是自己基本功差,但是yii的学习本身也需要成本

  2. yii学习笔记(1),目录结构和请求过程

    最近找找工作面试,发现很多要求会yii.于是准备学习一个新的框架 先在腾讯课堂找了个视频看了一下,然后去网上现在了“归档文件”(还有一种方式是通过php的包管理工具“composer”安装) 归档文件 ...

  3. yii学习笔记(四)

    return $this->goBack(); // 先看看Yii::$app->user->returnUrl是否已经设置, returnUrl没有设置且goBack()中的参数也 ...

  4. YII学习笔记-登录后的session的总结

    在YII框架的默认的登录后的session数据是id,name,__states这三个数据. 在搭配好YII框架环境后,可以使用admin/admin,来登录系统.如果在protected/views ...

  5. Yii学习笔记之三(在windows 上安装 advanced )

    首先说一下下载地址: http://www.yiiframework.com/download/ 然后将下载下来的文件进行解压到 你指定的文件夹 解压过程中假设报什么错误 直接忽略掉 我的解压文件夹是 ...

  6. yii学习笔记--快速创建一个项目

    下载yii框架 下载地址:http://www.yiiframework.com/ 中文网站:http://www.yiichina.com/ 解压文件

  7. yii学习笔记--url解析

    在通过yiic命令生成了一个app之后,我们通过浏览器访问会看到这样的一个页面.   点击home时,url为:http://localhost/blog/index.php?r=site/index ...

  8. yii学习笔记(2),创建控制器

    将网站根目录配置到项目的web目录 打开网站访问的是web/index.php这时打开默认页面 访问一下其他页面,发现浏览器地址的url携带了一个参数 http://www.test.com/inde ...

  9. Yii学习笔记之二(使用gii生成一个简单的样例)

    1. 数据库准备 (1) 首先我们建一数据库 yii2test 并建立一张表例如以下: DROP TABLE IF EXISTS `posts`; CREATE TABLE `posts` ( `po ...

随机推荐

  1. java基础温习 -- Thread

    启动线程两种方式: 1. 实现Runnable接口: 2. 继承Thread类. 选用:能使用接口,就不用从Thread类继承.    使用继承的方法不够灵活,从这个类继承了就不能从其他类继承: 实现 ...

  2. Win7下SQLServer访问虚拟机上的MySQL

    一.确保Win7能telnet通MySQL端口,防火墙设置可参考http://www.cnblogs.com/ShanFish/p/6519950.html二.配置系统DSN1.在Win7上安装MyS ...

  3. vue-cli的使用指南

    vue-cli 2.0 安装vue-cli npm install -g vue-cli 创建一个项目模板 vue init <template-name> <project-nam ...

  4. redis Hash 命令

    HDEL key field2 [field2] 删除一个或多个哈希表字段 HEXISTS key field 查看哈希表 key 中,指定的字段是否存在. HGET key field 获取存储在哈 ...

  5. pyinstaller 打包python3.6文件成exe 运行

    1.安装pyinstaller  切换到安装目录下script 运行  如我的目录:F:\Program Files\Python36\Scripts pip  install pyinstaller ...

  6. 19-10-23-L-Mor

    ZJ一下: 挺好,T2打表差点出规律(最近拿PFGYL硬卡提升自己几乎没有的打表水平) T1竟然……是个××题 T3的Floyd写死了. T1 简单思考会发现……直接全异或起来就AC 话说T1真叫最大 ...

  7. 多表关联懒加载导致的org.hibernate.LazyInitializationException: could not initialize proxy - no Session

    本来考虑的是懒加载,这样可以提高效率,不过由于时间紧急 把懒加载改为急加载临时解决 https://www.jianshu.com/p/89520964f458 自己管理session也可以 临时补丁 ...

  8. 抽象工厂模式(Abstract Factory)(抽象化)

    不管是简单工厂模式还是工厂方法模式,在整个模式中只能有一个抽象产品,但在现实生活中,一个工厂只创建单个产品的例子很少,因为现在的工厂都是多元化发展. (1)产品等级结构:即产品的继承结构,如一个抽象类 ...

  9. SVN添加自动忽略文件.settings .project .classpath target等

    eclipse svn提交忽略文件及文件夹,ignore设置无效.. 一.方法: 1. 将文件夹或文件从Eclipse中删除.记得要在Eclipse中删除,而不是Windows文件管理界面删除. 2. ...

  10. CF538G (套路题)

    CF538G 题目大意 你有一个长度为\(l\)的指令序列,每个指令为向上,向下,向左,向右中的一种. 机器人会循环执行该序列,即,执行完第\(l\)个指令后,就会重新开始执行第一个指令. 现在,给你 ...