Yiimodel layer operation summary _ PHP Tutorial

Source: Internet
Author: User
Yiimodel layer operation summary. Yiimodel layer operation summary yiimodel layer operation attributes and methods summary. TableName-set the table name corresponding to the Model, for example: publicfunctiontableName () {returngshop_order_ext;} r yii model layer operation summary

Yii model layer operation attribute and method summary.

TableName-set the table name corresponding to the Model, for example:

Public function tableName () {return 'gshop _ order_ext ';}

Rules-set validation rules for fields in the Model

Relations-set Association rules

AttributeLabels-set the alias of each field

SafeAttributes-set the attributes that can be modified

BeforeValidate and afterValidate-functions executed before and after field verification must return true

BeforeSave and afterSave-to record the functions executed before and after storage, return true

Second, the ORM in Yii uses AR, and there are several main operations:

Save-Operation Data

Update-modify data

Delete-delete data

Validate-verify data

When reading a record, you can use the following methods:

FindByPk-searches for records using the primary key and returns a single record

FindByAttribute-query a record through the attribute and the result is a single record.

FindAllByAttributes-query data through attributes and the result is a record set

FindAll-query data through the CDbCriteria object and the result is a record set *

There are two types of parameters received by the lookup method. if there is no asterisk, the array is used as the parameter, and the received CDbCriteria object with a star number is used as the parameter, more search conditions can be provided when the object is used, an example is provided below:

$ Criteria = new CDbCriteria; // Create a CDbCriteria object
$ Criteria-> condition = 'title LIKE % '. 'php'.' % '; // Set query conditions
$ Criteria-> order = 'createdtime desc'; // you can specify a sorting condition.
$ Criteria-> limit = 10; // limit the number of records
$ Criteria-> select = 'id, title, content'; // set the fields contained in the result

$ Articles = Article: model ()-> findAll ($ criteria); // The result is an array, where each element is a record object.

Again, Yii uses LazyLoad to load associated data by default. in this way, when we do not need to associate data, Yii will not help us read, greatly accelerating the response speed. however, sometimes we need to associate data. for example, when reading an article, we need the classification of the article. if LazyLoad is used, how many items are required, the efficiency of queries is very low. in this case, EagerLoad is required, that is, all data in the associated table is read at one time.

For example:

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

Use with to read all the associated table data at a time. the associated table settings are set in the relation in the Model.

For example:

public function relations() {       return array(             'category' => array(self::BELONGS_TO, 'Category', 'categoryId'),       );}

Very clear.

Articles you may be interested in
  • Module development and analysis of Yii framework
  • Yii Framework Yiiapp ()
  • Yii database addition, modification, and deletion operations summary
  • Yii database query operation summary
  • Summary of common Yii CDbCriteria methods
  • Yii gets the current controller name and action name
  • Yii using PHPExcel to import Excel files
  • Yii action methods and techniques

Http://www.bkjia.com/PHPjc/1058850.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1058850.htmlTechArticleyii model layer operation summary yii model layer operation properties and methods summary. TableName-set the table name corresponding to the Model, for example: public function tableName () {return 'gshop _ order_ext ';} r...

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.