Home > Backend Development > PHP Tutorial > Summary of yii model layer operations_PHP tutorial

Summary of yii model layer operations_PHP tutorial

WBOY
Release: 2016-07-12 09:07:56
Original
896 people have browsed it

Yii model layer operation summary

Yii model layer operation attributes and methods summary.

tableName – Set the table name corresponding to the Model, for example:

public function tableName(){return 'gshop_order_ext';}

rules – Set validation rules for each field in the Model

relations – Set association rules

attributeLabels – Set aliases for each field

safeAttributes – Set fields that can modify attributes

beforeValidate and afterValidate – functions executed before and after field validation, which need to return a true value

beforeSave and afterSave – records the functions executed before and after storage, and needs to return a true value

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

save – operation data

update – modify data

delete – delete data

validate – Validate data

When reading records, there are several methods:

findByPk – Find a record by primary key, the result is a single record

findByAttribute – Find a record by attribute, the result is a single record

findAllByAttributes – Find data by attributes, the result is a recordset

findAll – Find data through CDbCriteria object, the result is a record set *

There are two types of parameters received by the search method. The one without an asterisk accepts an array as a parameter, and the one with an asterisk receives a CDbCriteria object as a parameter. When using an object, more search conditions can be provided. An example is given below. :

$criteria = new CDbCriteria; // Create CDbCriteria object
$criteria->condition = 'title LIKE %' . 'php' . '%'; // Set query conditions
$criteria->order = 'createdTime DESC'; // Set sorting criteria
$criteria->limit = 10; //Limit the number of records
$criteria->select = 'id,title,content'; // Set the fields included in the result

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

Again, Yii adopts the LazyLoad loading method for related data by default, that is, it reads it only when needed. In this way, when we do not need the related data, Yii will not help us read it, which greatly speeds up the response. Speed. But there are also times when we need to associate data. For example, when reading an article, we need the category to which the article belongs. If we use LazyLoad, how many articles will there be and how many times will be queried, which is very inefficient. , then EagerLoad is needed, that is, reading all the data in the related table at one time.

For example:

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

Use with to read all the data of the related table at once. The settings of the related table are set in the relationship in the Model.

For example:

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

Very clear and concise.

Articles you may be interested in

  • Yii framework module development analysis
  • Understanding of Yii framework Yiiapp()
  • yii database addition , modification, deletion related operations summary
  • yii database query operation summary
  • Summary of common methods of Yii CDbCriteria
  • Yii Get the current controller name and action name
  • Yii uses PHPExcel to import Excel files
  • Yii action method tips

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1058850.htmlTechArticleYii model layer operation summary yii model layer operation attributes and methods summary. tableName – Set the table name corresponding to the Model, for example: public function tableName(){return 'gshop_order_ext';} r...
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template