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

Yii笔记

本文纯粹是笔记,记下来怕忘了
1、终止当前操作,一般情况下是用exit,但用了Exit后无法看到trace的内容。所以要采用Yii::app()->end();
2、createUrl和createAbsoluteUrl,这两个函数,如果需要宣传自己的网址和内容,还是用createAbsoluteUrl函数吧。里面带了全部路径,而CreateUrl只会显示/home/xxx这样的路径,不利于SEO
3、widget默认的render无法使用theme中的模版,于是写一个类继承自CWidget,然后所有的widget都继承这个类,比如我这样实现了。。

PHP代码
  1. class Widget extends CWidget{  
  2.     private static $_viewPaths;  
  3.     /** 
  4.      * 重写CWidget中的getViewPath 
  5.      */  
  6.     public function getViewPath($checkTheme=false)  
  7.     {  
  8.         $className=get_class($this);  
  9.         if(isset(self::$_viewPaths[$className]))  
  10.             return self::$_viewPaths[$className];  
  11.         else  
  12.         {          
  13.             if($checkTheme && ($theme=Yii::app()->getTheme())!==null){  
  14.                 return self::$_viewPaths[$className] = $theme->getBasePath().DIRECTORY_SEPARATOR."widgets";  //这是我自己的路径
  15.             }  
  16.             $class=new ReflectionClass($className);  
  17.             return self::$_viewPaths[$className]=dirname($class->getFileName()).DIRECTORY_SEPARATOR.'views';              
  18.         }  
  19.     }  
  20. }  

覆写父类方法,使得getViewPath找到我指定的目录,我目录在themes/classic/下建了一个widgets的目录。这样的好处很多。。而且对于CSS和JS等路径都可以和标准视图一致

先写这么多。。。

Tags: yii

怒了,QueryCache

怒了。真的怒了。。。。
前两天我还一直在想,怎么让yii实现查询的时候进行缓存,当时考虑在beforeFind和afterFind后做处理,但看了代码才发现beforeFind,如果不显式传入criteria,就没有办法把查询参数传递进去,那我就没有办法判断当前查询的param 是否被缓存了。AfterFind,倒是可以做处理,因为$event->sender或者$this中都已经有了结果集了。。。但没有办法判断是否缓存,查询完的缓存意义也就不大了。不是吗?

刚才无意中看了一下Yii 1.1.17的changelog,居然看到了这个:query-caching,当时我就震精了。http://www.yiiframework.com/doc/guide/1.1/en/caching.data#query-caching

好象这次1.1.17更新的几个功能都不错。。。查看这里:http://www.yiiframework.com/news/45/yii-php-framework-1-1-7-is-released/

  1. RESTful URL Support
  2. Query Caching
  3. Parameter Binding for Class-based Actions
  4. Seamless Client-side Validation
  5. Passing Parameters to Relational Named Scopes
  6. Using 'through' with HAS_MANY and HAS_ONE
  7. Using Transactions in DB Migration
  8. Registering and Using Custom Script Packages

好象,除了1、7我兴趣不是特别大,其它的几个都不错,原来的Actions中的Action无法绑定参数的,现在也直接可以用了,也就是说Action可以更容易的被移出。代码也更易分离了。4是客户端验证,原来还要在提交后到服务端验证,现在又方便一点了。。
其它的还是看看官方吧,不一定介绍了。

Tags: yii, querycache

zend newsletter

收邮件的时候,收到了这封zend framework更新的信息,果然不出我所料,ZF要升级了,但究竟什么 时候能够出来,还要等啊

官方发出一条信息关于zend framework2模式的网络会议。它说:

XML/HTML代码
  1. Zend Framework 1 grew organically. An effort behind version 2.0 was to identify common themes and patterns, and to unify these in order to provide common low-level patterns to use everywhere.  
  2.   
  3. Presented by Matthew Weier O'Phinney, this webinar will serve as an overview of these patterns, identifying them, and discussing the interfaces involved and the concrete use cases we plan to ship in ZF2. Matthew will also show how you might create your own implementations and slip-stream them into your application. Amongst the patterns discussed will be events, brokers, and dispatchers.  
如果有兴趣的话还可以参加讨论。。。March 30,不知道讨论结束后会有怎么样的一个结果。比如2的模式是否与1一样,不审会有一些其他的构思,需要期待了

 

Tags: zend framework

Yii:relations update(self::STAT)

今天在群里有朋友问我yii的relations的问题,结果贴出来的代码居然是类似这样的:

PHP代码
  1. function relations(){  
  2.     return array(  
  3.         'xx'=>array(self::STAT,'aa','aa_id')  
  4.     );  
  5. }  

我当时直接了当的对他说,错了,怎么可能会有self::STAT呢?只有has_many,has_one,belongs_to,many_many,stat是错的。结果他回答说我OUT了,STAT是新加入的。。。

于是欣欣然跑到YII的官网查看了一下GUIDE,果然在 Relational Active Record,一节中发现了这个Statistical Query

官方怎么说来着?第一句就让我大吃一惊:

Note: Statistical query has been supported since version 1.0.4.

奇怪,以前怎么没注意?

Besides the relational query described above, Yii also supports the so-called statistical query (or aggregational query). It refers to retrieving the aggregational information about the related objects, such as the number of comments for each post, the average rating for each product, etc. Statistical query can only be performed for objects related in HAS_MANY (e.g. a post has many comments) or MANY_MANY (e.g. a post belongs to many categories and a category has many posts).

Performing statistical query is very similar to performing relation query as we described before. We first need to declare the statistical query in the relations() method of CActiveRecord like we do with relational query.

PHP代码
  1. class Post extends CActiveRecord  
  2. {  
  3.     public function relations()  
  4.     {  
  5.         return array(  
  6.             'commentCount'=>array(self::STAT, 'Comment''post_id'),  
  7.             'categoryCount'=>array(self::STAT, 'Category''post_category(post_id, category_id)'),  
  8.         );  
  9.     }  
  10. }  

In the above, we declare two statistical queries: commentCount calculates the number of comments belonging to a post, and categoryCount calculates the number of categories that a post belongs to. Note that the relationship between Post and Comment is HAS_MANY, while the relationship between Post and Category is MANY_MANY (with the joining table post_category). As we can see, the declaration is very similar to those relations we described in earlier subsections. The only difference is that the relation type is STAT here.

With the above declaration, we can retrieve the number of comments for a post using the expression $post->commentCount. When we access this property for the first time, a SQL statement will be executed implicitly to retrieve the corresponding result. As we already know, this is the so-called lazy loading approach. We can also use the eager loading approach if we need to determine the comment count for multiple posts:

PHP代码
  1. $posts=Post::model()->with('commentCount''categoryCount')->findAll();  

The above statement will execute three SQLs to bring back all posts together with their comment counts and category counts. Using the lazy loading approach, we would end up with 2*N+1 SQL queries if there are N posts.

By default, a statistical query will calculate the COUNT expression (and thus the comment count and category count in the above example). We can customize it by specifying additional options when we declare it in relations(). The available options are summarized as below.

  • select: the statistical expression. Defaults to COUNT(*), meaning the count of child objects.

  • defaultValue: the value to be assigned to those records that do not receive a statistical query result. For example, if a post does not have any comments, its commentCount would receive this value. The default value for this option is 0.

  • condition: the WHERE clause. It defaults to empty.

  • params: the parameters to be bound to the generated SQL statement. This should be given as an array of name-value pairs.

  • order: the ORDER BY clause. It defaults to empty.

  • group: the GROUP BY clause. It defaults to empty.

  • having: the HAVING clause. It defaults to empty.

关键的是看最后这几个条件,可以让你知道这些参数如何配置。。。
官方的GUIDE地址:http://www.yiiframework.com/doc/guide/1.1/en/database.arr

Tags: yii, relations, stat, static query

Yii的config配置的一些记录

用YII开发的时候,由于会用到gii之类的工具,所以assets目录,仿佛就一定会需要存在了,但是assets这个目录名,会可能与我们自己的一些image,css之类的目录存在冲突(我是指目录太多了,不易管理,虽然这个目录似乎也不需要管理)
这时候,我们可以通过更改config来设置。
在config的components里加入:

XML/HTML代码
  1. 'assetManager'=>array(  
  2.     'basePath' => 'xxxxxx/xxx'  
  3. )  

OK,我就这样把它与我存放image/css/js的目录合并在一起了。

模版,如果不想过多的折腾,我想,官方推荐的prado这样的伪PHP模版其实也不错,最起码常用的操作都封装了,也不用担心与其他的模版是否会存在这样那样的冲突,最起码他们的整合应该算是最好的。所以。。。加上:

PHP代码
  1. 'viewRenderer' => array(  
  2.     'class' => 'CPradoViewRenderer',  
  3. ),  

不用过多的担心效率,它也是编译到runtime目录下执行的。。。

params,这个嘛。。。直接引用文件吧,其实在自动生成的代码里,就是这样配置的,直接引用文件有一个好处就是,你可以把系统的配置信息扔到一个文件里,然后让params来加载。就方便很多了。

Tags: yii, config