手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆
浏览模式: 标准 | 列表Tag:yii

Yii框架中设置时区

时区这东西,在开发的时候,你说重要吧,也还好,毕竟没它也能正常运行,你说不重要吧,那就纠结了。特别是linux系统,都TMD差上几小时,你能不痛苦吗?win还好一点。
有一些常规方法,是大家目前都在采用的
1、php.ini中的设置,这个就不谈了,
2、程序中公用文件里设置,date_default_timezone_set一下时区
3、或者。。。自己写时间处理函数,在遇到时间的时候,用这个函数处理(比较偏向于使用这种方式,如果你的用户来自于五湖四海,或许就有用了)
4、yii框架中,可以直接在/protected/config/main.php中加入timeZone,即可(补充:Yii2方法也是一样,只是config文件的位置不太一样罢了)

 

Tags: yii, 时区

Yii的AR效率释疑

关于yii的AR效率,其实一直以来都有很多的想法,试想,如果不做metadata的缓存,每次查询前,都必须先做一下metadata的查询,效率也不会太高到哪里吧?
所以,看到官方有人在问就关注了一下:http://www.yiiframework.com/forum/index.php/topic/16597-yii%E7%9A%84ar%E7%9C%9F%E7%9A%84%E8%83%BD%E7%94%A8%E4%B9%88%EF%BC%9F/

有人提问:

XML/HTML代码
  1. 我在首页会很多个ar的请求。热门文章、最新文章、编辑推荐、最新评论的文章。。。。  
  2.   
  3. 我把这些通过 model()->findAll()的结果放在一个数组里。  
  4.   
  5. 然后把这个数组var_dump了一下,这些数据高达1.06M。这是不是太耗内存了?  
  6.   
  7. 页面上全是密密麻麻的数据表结构,如果关联关系复杂点的,会更多。  
  8.   
  9. 我光var_dump(yii::app()->db) 就有5000多行的数据。  
  10.   
  11.   
  12. 这样的db操作这么耗内存,敢用么?谁能消除我这个担心?多谢  
  13.   
  14. AcitveRecord ,只能玩一玩开拓眼界. 真要生产环境用, 就不行!   

然后,Qiang就回复了:

XML/HTML代码
  1. 你的结论有点想当然了。建议你仔细做profiling来验证。事实上,AR已经被成功应用在若干大流量的网站上了。  
  2.   
  3. 你不能用var_dump()来估计AR的内存开销。var_dump会把所有被reference到的对象都dump出来,包括application,以及所有的application component,因为它们都被AR间接reference到了。  
  4.   
  5. AR额外的内存开销是存储10个左右变量的开销。如果按20字节来估算,额外的开销是200字节,这样在1000个AR对象情况下,额外的内存开销为200KB。这个对几乎所有的web应用而言应该不是问题。  
  6.   
  7. 如果你需要装载更多的数据,建议你使用DAO。  
  8.   
  9. 另外,对于任何大流量的web应用而言,cache都是必须的。cache可以帮助解决绝大多数的性能瓶颈。   

cache在DB中有一些处理的,比如那个duration,在db中查询就可以做一下缓存了。然后metadata再做一下缓存,不也挺好?

Tags: yii

Yii Behavior的简单用法

Yii的behavior用起来是十分方便的,官方的文档也很多,我不多嘴一一解释,我只说一些简单的用法
如果你看过PHP5.4,你应该知道5.4多了个新功能traits。
那你可以对着手册看了,你就当yii的behavior就是5.4的traits。

在任何基于CComponents类扩展的类里,都可以用attachBehavior来附加一个行为,这就象5.4的在类里面 use traits类一样
附加行为后,直接可以$this->行为中的方法,嗯,这个与traits也一样。
好吧,来个简单的例子:

PHP traits
  1. <?php  
  2. class Base {  
  3.     public function sayHello() {  
  4.         echo 'Hello ';  
  5.     }  
  6. }  
  7.   
  8. trait SayWorld {  
  9.     public function sayHello() {  
  10.         parent::sayHello();  
  11.         echo 'World!';  
  12.     }  
  13. }  
  14.   
  15. class MyHelloWorld extends Base {  
  16.     use SayWorld;  
  17. }  
  18.   
  19. $o = new MyHelloWorld();  
  20. $o->sayHello();  
  21. ?>  

Yii的用法:

PHP代码
  1. <?php  
  2. class xxx extends CBehavior  
  3. {  
  4.     public function show(){  
  5.         echo "show";  
  6.     }  
  7. }  
  8.   
  9. class test extends CComponents  
  10. {  
  11.     public function hello(){  
  12.         $this->attachBehavior('唯一标记符',"xxx");  
  13.         $this->show();  
  14.     }  
  15. }  

看看,是不是用法一样?不过这样也带来一个问题。。。TMD,没法在IDE里面自动识别了。

好吧,只能这样折腾自己了。。忍忍。

Tags: yii, behavior

yii 连接 mssql遇到的一些问题

1、yii在linux下面连接mssql时,connectString一般是dblib:xxxxx,而不是mssql,这是因为用freetds的原因
2、用gii生成mssql的数据表结构时,如果表的字段是类似"Post Time"(即,字段中间有空格),则直接会报错
3、当生成好Model时,使用xxx::model()->findAll()时,如果最初的表结构里有允许某个字段为空,即允许isnull的话,在数据库中,该字段无值时是NULL,findAll就会报错:

XML/HTML代码
  1. CDbCommand failed to execute the SQL statement: SQLSTATE[HY000]: General error: 4004 General SQL Server error: Check messages from the SQL Server [4004] (severity 16) [(null)]. The SQL statement executed was: SELECT TOP 1 * FROM [dbo].[DatabaseLog] [t]  

4、无论是mysql还是sqlserver,当在components中设置columnCase值的时候,都会报错,这是因为在 MysqlSchema中都是采用了大小写敏感的方式(如$column['IsIdenfy'])之类的,当大小写敏感后,如果在attributes 中设置了PDO的ATTR_CASE,都会报错

Tags: yii, mssql

yii:Apache and Nginx configurations

yii的urlmanager可以让项目在访问的时候隐藏index.php,也可以以更优雅的urlrewrite方式来显示,但这一切需要一些配置,在apache上的配置上就相对比较简单,直接参考wordpress的官方配置就完了,但其实很久以来,一直都没有人写过nginx下的配置。大家都是在根据wordpress配置来更改的,比如lnmp项目中,就是:

XML/HTML代码
  1. location / {  
  2. if (-f $request_filename/index.html){  
  3.                 rewrite (.*) $1/index.html break;  
  4.         }  
  5. if (-f $request_filename/index.php){  
  6.                 rewrite (.*) $1/index.php;  
  7.         }  
  8. if (!-f $request_filename){  
  9.                 rewrite (.*) /index.php;  
  10.         }  
  11. }  

不过,这两天在看官方guide的文档,原来这些问题,官方已经提供方案了:http://yii.neatcn.com/doc/guide/1.1/en/quickstart.apache-nginx-config#nginx

1. Apache

Yii is ready to work with a default Apache web server configuration. The .htaccess files in Yii framework and application folders restrict access to the restricted resources. To hide the bootstrap file (usually index.php) in your URLs you can add mod_rewrite instructons to the .htaccess file in your document root or to the virtual host configuration:

XML/HTML代码
  1. RewriteEngine on  
  2.   
  3. # if a directory or a file exists, use it directly  
  4. RewriteCond %{REQUEST_FILENAME} !-f  
  5. RewriteCond %{REQUEST_FILENAME} !-d  
  6. # otherwise forward it to index.php  
  7. RewriteRule . index.php 

2. Nginx

You can use Yii with Nginx and PHP with FPM SAPI. Here is a sample host configuration. It defines the bootstrap file and makes yii catch all requests to unexisting files, which allows us to have nice-looking URLs.

XML/HTML代码
  1. server {  
  2.     set $host_path "/www/mysite";  
  3.     access_log  /www/mysite/log/access.log  main;  
  4.   
  5.     server_name  mysite;  
  6.     root   $host_path/htdocs;  
  7.     set $yii_bootstrap "index.php";  
  8.   
  9.     charset utf-8;  
  10.   
  11.     location / {  
  12.         index  index.html $yii_bootstrap;  
  13.         try_files $uri $uri/ $yii_bootstrap?$args;  
  14.     }  
  15.   
  16.     location ~ ^/(protected|framework|themes/\w+/views) {  
  17.         deny  all;  
  18.     }  
  19.   
  20.     #avoid processing of calls to unexisting static files by yii  
  21.     location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)$ {  
  22.         try_files $uri =404;  
  23.     }  
  24.   
  25.     # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000  
  26.     #  
  27.     location ~ \.php {  
  28.         fastcgi_split_path_info  ^(.+\.php)(.*)$;  
  29.   
  30.         #let yii catch the calls to unexising PHP files  
  31.         set $fsn /$yii_bootstrap;  
  32.         if (-f $document_root$fastcgi_script_name){  
  33.             set $fsn $fastcgi_script_name;  
  34.         }  
  35.   
  36.         fastcgi_pass   127.0.0.1:9000;  
  37.         include fastcgi_params;  
  38.         fastcgi_param  SCRIPT_FILENAME  $document_root$fsn;  
  39.   
  40.         #PATH_INFO and PATH_TRANSLATED can be omitted, but RFC 3875 specifies them for CGI  
  41.         fastcgi_param  PATH_INFO        $fastcgi_path_info;  
  42.         fastcgi_param  PATH_TRANSLATED  $document_root$fsn;  
  43.     }  
  44.   
  45.     location ~ /\.ht {  
  46.         deny  all;  
  47.     }  
  48. }  

Using this configuration you can set cgi.fix_pathinfo=0 in php.ini to avoid many unnesessary system stat() calls.

Tags: yii, apache, nginx, wordpress, urlrewrite