PHP Development Framework Yii Framework Tutorial (Data provider Introduction)

Source: Internet
Author: User
Tags arrays count documentation sort yii

This begins with the introduction of the Zii component, a brief introduction to the data source interface supported by Yii Idataprovider,idataprovider The main function is to provide a data source for UI components such as Gridview,listview, and also to support the paging and sorting of data. The following figure is the three types of data sources that are built in Yii:

Cactivedataprovider a data source based on an active record

Carrarydataprovider data sources based on arrays

Csqldataprovider a data source based on SQL queries

The three kinds of data provider are used in a similar way:

Cactivedataprovider is based on ActiveRecord, which reads database records through the AR Cactiverecord::findall method and sets the query criteria through the criteria property.

Such as:

$dataProvider =new 

cactivedataprovider (' Post ', Array ('     
    criteria ' =>array) ('     
        condition ' => ' status= 1 ',     
        ' order ' => ' Create_time DESC ',     
        ' with ' =>array (' author '),     
    ' pagination ' =>array (     
        ' PageSize ' =>20,))     
;     
$dataProvider->getdata () would return a list of Post objects

Carraydataprovider is based on an array where the properties RawData set raw data, typically an array or DAO query results, such as:

$rawData =yii::app ()->db->createcommand (

' SELECT * from Tbl_user ')->queryall ();     
or using: $rawData =user::model ()->findall ();     
$dataProvider =new Carraydataprovider ($rawData, Array (     
    ' id ' => ' user ',     
    ' sort ' =>array (     
        ' Attributes ' =>array (     
             ' id ', ' username ', ' email ',     
        ),     
    ),     
    ' pagination ' =>array (     
        ' pageSize ' =>10,))     
;     
$dataProvider->getdata () would return a list of arrays.

Csqldataprovider is based on SQL queries and is configured by setting up SQL statements, such as:

$count =yii::app ()->db->createcommand (' SELECT count (*) from Tbl_user ')-

>queryscalar ();     
$sql = ' SELECT * from Tbl_user ';     
$dataProvider =new Csqldataprovider ($sql, Array (     
    ' Totalitemcount ' => $count,     
    ' sort ' =>array (     
        ' Attributes ' =>array (     
             ' id ', ' username ', ' email ',     
        ),     
    ),     
    ' pagination ' =>array (     
        ' pageSize ' =>10,))     
;     
$dataProvider->getdata () would return a list of arrays.

For Dataprovider detailed documentation, see YII Documentation: Http://www.yiiframework.com/doc/api/1.1/CDataProvider

See a full set of tutorials: http://www.bianceng.cn/webkf/PHP/201301/35265.htm

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.