国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

Home php教程 PHP開發(fā) Detailed explanation of relations data correlation query and statistical function usage in Yii

Detailed explanation of relations data correlation query and statistical function usage in Yii

Dec 30, 2016 pm 04:15 PM

The examples in this article describe the usage of relations data correlation query and statistical functions in Yii. Share it with everyone for your reference, the details are as follows:

Related query, Yii also supports so-called statistical query (or aggregate query). It refers to retrieving aggregated information about associated objects, such as the number of comments for each post, the average rating of each product, etc. Statistical queries are only performed on objects associated with HAS_MANY (for example, a post has many comments) or MANY_MANY (for example, a post belongs to many categories and a category has many posts).

Performing statistical queries is very similar to the correlation query described previously. We first need to declare the statistical query in the relations() method of CActiveRecord.

class Post extends CActiveRecord
{
  public function relations()
  {
    return array(
      'commentCount'=>array(self::STAT, 'Comment', 'post_id'),
      'categoryCount'=>array(self::STAT, 'Category', 'post_category(post_id,category_id)'),
    );
  }
}

Related query namespace

Related queries can also be executed with namespaces. There are two forms. In the first form, the namespace is applied to the main model. In the second form, the namespace is applied to the associated model.

The following code shows how to apply the namespace to the main model.

$posts=Post::model()->published()->recently()->with('comments')->findAll();

This is very similar to a non-correlated query. The only difference is that we use the with() call after the namespace. This query should return recently published posts and their comments.

The following code shows how to apply a namespace to an associated model.

$posts=Post::model()->with('comments:recently:approved')->findAll();

The above query will return all posts and their moderated comments. Note that comments refers to the association name, while recently and approved refer to the namespace declared in the Comment model class. Association names and namespaces should be separated by colons.


Namespaces can also be specified in the with option of association rules declared in CActiveRecord::relations(). In the example below, if we access $user->posts, it will return all moderated comments for this post.

class User extends CActiveRecord
{
  public function relations()
  {
    return array(
      'posts'=>array(self::HAS_MANY, 'Post', 'author_id', 'with'=>'comments:approved'),
    );
  }
}

I hope this article will be helpful to everyone’s PHP program design based on the Yii framework.

For more detailed explanations on the usage of relations data correlation query and statistical functions in Yii, please pay attention to the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1502
276