Relations Data Association Query and statistical functional usage in Yii _php example

Source: Internet
Author: User
Tags smarty template yii

This article illustrates the Relations Data Association query and statistical functional usage in YII. Share to everyone for your reference, specific as follows:

Associated queries, YII also supports so-called statistical queries (or aggregation queries). It refers to the retrieval of aggregated information about the associated object, such as the number of comments per post, the average level of each product, and so on. A statistical query is performed by 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 post) associated objects.

Performing a statistical query is very similar to the associated query described earlier. We first need to declare a 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) '),
    );
  }


Associating query namespaces

Associated queries can also be executed with namespaces. There are two kinds of forms. In the first form, namespaces are applied to the main model. In the second form, namespaces are applied to the association model.

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

Copy Code code as follows:
$posts =post::model ()->published ()->recently ()->with (' comments ')->findall ();

This is very similar to a query that is not associated. The only difference is that we use the WITH () call after the namespace. This query should return the most recently published post and their comments.

The following code shows how to apply namespaces to the association model.

Copy Code code as follows:
$posts =post::model ()->with (' comments:recently:approved ')->findall ();

The above query will return all post and their audited comments. Note that comments refers to the associated name, whereas recently and approved refer to namespaces declared in the Comment model class. The associated name and namespace should be delimited by a colon.

Namespaces can also be specified in the WITH option of association rules declared in Cactiverecord::relations (). In the following example, if we visit the $user->posts, it will return all the audited comments for this post.

Class User extends Cactiverecord
{public
  function relations ()
  {return
    array (
      ' posts ' =>array (Self::has_many, ' Post ', ' author_id ', ' with ' => ' comments:approved ')
    ;
  }


For more information on YII-related content, readers who are interested in this site can view the topics: Introduction to YII Framework and summary of common skills, "Summary of PHP Excellent development framework", "Smarty Template Introductory Course", "Introduction to PHP object-oriented programming", "PHP string" Summary of Usage , "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design based on the YII framework.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.