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

怒了,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

Yii ClinkPager 郁闷

开发的时候,分页用了CLinkPager,然而在某一个页面的时候,page一直在变,但是下面的分类中的当前页永远在1上,不随着页码的变化而变化。
开始的时候以为是分页程序有问题,仔细对应了一下,发现分页的数据是正确的。
排查了半个多小时,突然想起,会不会$_GET['page']被unset了?
找了一下页面,最后在模版页里居然真的发现了unset($_GET['page']),当时我就傻眼了。
顺便再上一个option的onchange切换函数。很烂,只求先解决问题。。。。

JavaScript代码
  1. function urlchange(field,value) {  
  2.     var href= location.href;  
  3.     var regex = new RegExp(field+"=[\-|0-9]{0,}");  
  4.     if(href.indexOf(field)!=-1){  
  5.         location.href = href.replace(regex,field+"="+value);  
  6.     }else{  
  7.         location.href = href + "&"+field+"="+value;  
  8.     }  
  9. }      

用法就是<select onchange="urlchange('page',this.value)"><option value='1'>第一页</option><option value='2'>第二页</option><option value='10'>第十页</option></select>

Tags: yii, clinkpager