于是欣欣然跑到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.
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:
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.
First off, all credits go to this guy. I’m just listing the steps on how I did it in Windows 7 with PHP 5.3. Also, I tested this using WampServer but I believe it should work on any PHP install.
Install memcached
Download the Memcached Win32 library here: http://code.jellycan.com/memcached. Just get the Win32 binary (direct link). Extract the downloaded archive file in a directory (e.g. c:\memcached). There should be a memcached.exe in there.
Run a command prompt as an administrator. Some info on how to do that here. 【可能就是因为这个,所以我无法装成服务???】
Install memcached as a service. Go to the memcached directory, type and run:
memcached -d install
If you get an error saying “MSVCP71.dll is missing”, see this page for a solution.
Start the memcached service by running:
memcached -d start
You can verify if memcached is running by executing this in the command line:
wmic process get description, executablepath | findstr memcached.exe
You should see a result list showing memcached.exe and its full path.
Install PHP Memcache extension (php_memcache.dll)
Chances are you don’t have php_memcache.dll in your PHP extensions yet. You can download a build of it here.Basu has noted in the comments that VC6 builds are no longer available from that link. You can download the correct build here.
The archive should contain php_memcache.dll. Extract the archive to your php extensions directory. On my system (WampServer), this was C:\wamp\bin\php\php5.3.0\ext.
Edit php.ini, add this line to enable the extension:
extension=php_memcache.dll
Or if you’re using WampServer, restart it and enable the extension through the WampServer system tray menu.