Yii Model Layer Operation Summary _php tutorial

Source: Internet
Author: User

Yii Model Layer Operation summary


A summary of the operation properties and methods of Yii model layer.

tablename– set the table name corresponding to the model, for example:

Public Function TableName () {return ' Gshop_order_ext ';}

rules– setting validation rules for each field in the model

relations– Setting Association Rules

attributelabels– setting aliases for each field

safeattributes– setting a field that can modify a property

Beforevalidate and aftervalidate– fields are validated before and after validation functions that need to return a true value

BeforeSave and aftersave– records pre-and post-store functions that need to return a true value

Second, the ORM in Yii uses AR, there are several major operations, namely:

save– Operating Data

update– Modifying data

delete– Deleting data

validate– Validating data

When reading records, there are several ways to do this:

findbypk– find records by primary key and result in a single record

findbyattribute– to find records through attributes, resulting in a single record

findallbyattributes– finding data through attributes, resulting in a recordset

findall– finding data through Cdbcriteria objects, resulting in Recordset *

The Find method receives the parameter to have 2 kinds, does not have the asterisk, accepts the array as the parameter, has the asterisk to receive the Cdbcriteria object as the parameter, when uses the object, may provide the more search condition, below gives an instance:

$criteria = new Cdbcriteria; Create a Cdbcriteria object
$criteria->condition = ' title like% '. ' PHP '. '%'; Set query criteria
$criteria->order = ' createdtime DESC '; Set sorting criteria
$criteria->limit = 10; Number of bars qualifying records
$criteria->select = ' id,title,content '; Set the fields that the result contains

$articles = Article::model ()->findall ($criteria); The result is an array, where each element is a Record object

Again, Yii defaults to the Lazyload loading form of the associated data, which is read only when needed. Thus, when we do not need to correlate the data, Yii will not help us to read, greatly accelerating the speed of the reaction. But there are some times, we need to correlate data, such as reading the article, we need the article belongs to the classification, if the use of Lazyload form, there will be how many, query how many times, the efficiency is very low, then need to eagerload, The data of the associated table is read out all at once.

Like what:

$articles = Article::model ()->with (' category ')->findall ();

Use with to read the data of the associated table all at once. The settings of the associated table are set in the relation of the model.

Like what:

Public Function relations () {       return Array (             ' category ' = = Array (self::belongs_to, ' category ', ' categoryId ') ,       );}

Very clear and unambiguous.

Articles you may be interested in

    • Analysis of module development of YII Framework Framework
    • The understanding of Yii Framework Yiiapp ()
    • Yii database Add, modify, delete related Operations summary
    • The summary of Yii database query operation
    • Summary of common methods of Yii Cdbcriteria
    • Yii gets the current controller name and action name
    • How Yii uses Phpexcel to import Excel files
    • Yii Action Method Tips

http://www.bkjia.com/PHPjc/1058850.html www.bkjia.com true http://www.bkjia.com/PHPjc/1058850.html techarticle the YII model layer operation summarizes the operation properties and methods of Yii model layer. tablename– sets the name of the table corresponding to the model, for example: Public function TableName () {return ' Gshop_order_ext ';} r ...

  • Related Article

    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.