Yii

Yii - How to print SQL used by findAll

你离开我真会死。 提交于 2019-12-30 05:55:06
问题 I have the following code to get some records from db $criteria = new CDbCriteria(); $criteria->condition = 't.date BETWEEN "'.$from_date.'" AND "'.$to_date.'"'; $criteria->with = array('order'); $orders = ProductOrder::model()->findAll($criteria); Is it possible to get the SQL that is used by the findAll? I know you can get it from the debug console. But I'm running the script in the background using yiic.php 回答1: You can log the executed queries in the application log and review that.

How to create a link with confirmation dialog using Yii?

寵の児 提交于 2019-12-30 03:49:07
问题 How can I create a link with a confirmation dialog in Yii framework? Let's say I have CHtml::link('Delete',array('wsrecruiteducation/delete','id'=>$model->EducID)); how do I convert that code snippet above, into a delete link with a confirm alert before deleting the data? 回答1: You just need to also use the last parameter of CHtml::link : CHtml::link( 'Delete', array('wsrecruiteducation/delete','id'=>$model->EducID), array('confirm' => 'Are you sure?') ); 回答2: you can do something like this:

php5.6+Redis+Windows7安装 (phpstudy)

北战南征 提交于 2019-12-30 02:23:35
1.首先去github网站上下载 https://github.com/dmajkic/redis/downloads ; 2.根据实际情况,将64bit的内容cp到自定义盘符目录,如D:\ Redis ; 3.打开cmd,cd/d 切换到Redis-server.exe目录,运行redis-server.exe redis.conf; 4.这时候另启一个cmd窗口,原来的不要关闭,不然就无法访问服务端了。 切换到redis目录下运行 redis-cli.exe -h 127.0.0.1 -p 6379 (-a password远端); 5.设置键值对 set myKey abc 取出键值对 get myKey; 这时候,windows环境下,redis的服务端和客户端都运行成功了。 ----------------------我们在 PHP 中安装redis扩展-------------------- 1.使用phpinfo()函数查看PHP的版本信息,这会决定扩展文件版本(特别注意以php版本的architecture是x86还是64为准,不能以 操作系统 为准); 2.根据PHP版本号,编译器版本号和CPU 架构 下载地址: http://windows.php .NET /downloads/pecl/snaps/redis/2.2.5/ http://windows

yii2使用 db log

僤鯓⒐⒋嵵緔 提交于 2019-12-29 22:31:45
在本地测试的时候,输出log,还是输出到db中比较顺手。 配置过程: 1.加入log组件的配置: 'log' =>[ # 追踪级别 # 消息跟踪级别 # 在开发的时候,通常希望看到每个日志消息来自哪里。这个是能够被实现的,通过配置 log 组件的 yii\log\Dispatcher::traceLevel 属性, 就像下面这样: 'traceLevel' => YII_DEBUG ? 3 : 0, # 通过 yii\log\Logger 对象,日志消息被保存在一个数组里。为了这个数组的内存消耗, 当数组积累了一定数量的日志消息,日志对象每次都将刷新被记录的消息到 log targets 中。 你可以通过配置 log 组件的 yii\log\Dispatcher::flushInterval 属性来自定义数量 'flushInterval' => 1, 'targets' => [ 'db' =>[ //'levels' => ['warning'], 'categories' => ['appadmin'], #'class' => 'yii\log\FileTarget', 'class' => 'yii\log\DbTarget', # 当 yii\log\Logger 对象刷新日志消息到 log targets 的时候,它们并 不能立即获取导出的消息。相反

yii2 HeadersAlreadySentException 报错

只愿长相守 提交于 2019-12-29 22:31:25
An Error occurred while handling another error: exception 'yii\web\HeadersAlreadySentException' with message 'Headers already sent in /xxxx/xxxx/xxx.php on line 90.' in /xxxx/xxxx/vendor/yiisoft/yii2/web/Response.php:366 Stack trace: #0 /xxxx/xxxx/vendor/yiisoft/yii2/web/Response.php(339): yii\web\Response->sendHeaders() #1 /xxxx/xxxx/vendor/yiisoft/yii2/web/ErrorHandler.php(135): yii\web\Response->send() #2/xxxx/xxxx/vendor/yiisoft/yii2/base/ErrorHandler.php(111): yii\web\ErrorHandler->renderException(Object(yii\web\HeadersAlreadySentException)) #3 [internal function]: yii\base\ErrorHandler-

YII2数据库操作出现类似Database Exception – yii\\db\\Exception SQLSTATE[HY000] [2002] No such file or director

杀马特。学长 韩版系。学妹 提交于 2019-12-29 22:30:50
参考文章:https://blog.csdn.net/zqtsx/article/details/41845511 我的系统时Ubuntu18使用上面的方法时发现,没有MySQL.socket,然后谷歌如apch何重建socket,找了半天感觉都是同一篇文章的不同转载,哎。。。   运行:phpinfo()只有MySQL的3个开发人,连上文的MySQL Support也没有 后来,想了一下,自己安装了Apache 和Mysql 以及apache的php模块,但是唯独没有自己安装PHP,之前在别的电脑上安装时,没有出现过这种错误,可能就是当时那个电脑上就已经有PHP了,于是开始按章PHP 参考文件:https://www.cnblogs.com/impy/p/8040684.html 对于这篇文章:我安装的是PHP7.2,最后没有安装:composer,我没用的着 然后,查看phpinfo()发现,MySQL Support模块出来了但是,还是没有显示MySQL.socket,路径,先刷新看看原始网页,看看有没有上面的错误, 刷新后,莫名其妙就没有了上面的问题,然后问题就解决了。。。 没有专门设置MySQL.socket, 来源: https://www.cnblogs.com/feiquan/p/10887994.html

yii2框架-yii2局部关闭(开启)csrf的验证

烈酒焚心 提交于 2019-12-29 17:11:26
(1)全局使用,我们直接在配置文件中设置enableCookieValidation为true [php] view plain copy request => [ 'enableCookieValidation' => true, ] 如果不需要使用csrf的话,设置'enableCookieValidation' => false,但是这是不安全的,因此yii2的yii\web\request中的enableCookieValidation默认设置为true的,也就是默认开启csrf的,所以我们也可以不配置这个值,默认开启。 如果开启csrf,因为这是全局的,所以在任何的post请求都会要求认证,所以我们在post数据的时候,就必须设置csrf的数据隐藏在表单中。 [php] view plain copy <input name="_csrf-backend" type="hidden" id="_csrf" value="<?php echo Yii::$app->request->csrfToken ?>">(每个人定论的csrf的name属性不一样,请自己参考自己的项目) post数据的时候必须要把这个值post过去,这个值的产生<?= Yii::$app->request->csrfToken ?>,返回一个加密后的csrfToken。

Yii如何使用数据库

萝らか妹 提交于 2019-12-29 16:33:46
1.Yii如何使用数据库 Yii通过数据库访问对象(Database Access Objects,简称DAO)来使用数据库的。 DAO建立在“PHP数据对象(PDO)之上,并提供一套面向对象的API来访问数据库” 2.数据库的连接 数据库的连接通常放在配置文件中,/common/config/main-local.php中。 'db' => [ 'class' => 'yii\db\Connection', 'dsn' => 'mysql:host=localhost;dbname=yii2advanced', 'username' => 'root', 'password' => 'root', 'charset' => 'utf8', ], 这里表示会创建一个yii\db\Connection对象,并用这个对象访问数据库。 这个数据库连接对象的写法:Yii->$app->db 连接不同数据库的语句略微有些差异。 3,数据库查询(yii\db\Command) 用SQL查询语句来创建一个yii\db\Command的对象,调用对象的方法来执行SQL查询,返回值是字符型的数组。 $post = Yii::$app->db->createCommand('SELECT * FROM post')->queryAll(); SQL语句还可以绑定参数。 $post = Yii::

Yii的学习(3)--查询生成器 (Query Builder)

大城市里の小女人 提交于 2019-12-29 16:06:32
原文地址: http://www.yiiframework.com/doc/guide/1.1/en/database.query-builder 不过原文是英文的,Yii的官网没有翻译这一章,自己就尝试着翻译了一下,有很多地方时会按自己的理解写,不一定完全按原文翻译。。。 Yii的查询生成器提供了用面向对象的方式写SQL语句。 它允许开发人员使用类的方法和属性来指定一个SQL语句的各个部分。 然后,组装成一个有效的SQL语句,可以通过调用DAO数据访问对象的描述方法为进一步执行。 以下显示了 一个 典型的使用查询生成器 建立一个select 语句 : $user = Yii::app()->db->createCommand() ->select('id, username, profile') ->from('tbl_user u') ->join('tbl_profile p', 'u.id=p.user_id') ->where('id=:id', array(':id'=>$id)) ->queryRow(); 当你在应用程序中需要组装SQL语句的程序,或基于一些条件逻辑时,最好使用查询生成器。 使用 查询生成器 的好处主要 包括 : 它可以建立复杂的SQL语句编程。 它会自引用表名和列名防止SQL保留字和特殊字符的冲突。 它还可以引用参数值和使用参数绑定

YII 数据库操作之DAO

房东的猫 提交于 2019-12-29 16:06:01
http://ioutsider.me/yii-shu-ju-ku-cao-zuo-zhi-dao.html 新增数据: Yii::app()->db->createCommand()->insert('es_users', array('name'=>'Outsider')); 删除数据: Yii::app()->db->createCommand()->delete('es_users', 'id=:id', array(':id'=>2)); 更新数据: $res = Yii::app()->db->createCommand()->update('es_users', array('name'=>'局外人'), 'id=:id',array(':id'=>2)); 查询数据: 查询一条记录 $row = Yii::app()->db->createCommand()->from('es_user')->where('id=:id',array(':id'=>2))->queryRow(); 查询符合条件的多条记录: Yii::app()->db->createCommand()->select('id,name,email')->from('es_user')->where('id>:id and id<5',array(':id'=>1))->queryAll(); 来源