Summary of yii model layer operations_PHP tutorial
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();
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'), ); }
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











Unfortunately, people often delete certain contacts accidentally for some reasons. WeChat is a widely used social software. To help users solve this problem, this article will introduce how to retrieve deleted contacts in a simple way. 1. Understand the WeChat contact deletion mechanism. This provides us with the possibility to retrieve deleted contacts. The contact deletion mechanism in WeChat removes them from the address book, but does not delete them completely. 2. Use WeChat’s built-in “Contact Book Recovery” function. WeChat provides “Contact Book Recovery” to save time and energy. Users can quickly retrieve previously deleted contacts through this function. 3. Enter the WeChat settings page and click the lower right corner, open the WeChat application "Me" and click the settings icon in the upper right corner to enter the settings page.

Tomato Novel is a very popular novel reading software. We often have new novels and comics to read in Tomato Novel. Every novel and comic is very interesting. Many friends also want to write novels. Earn pocket money and edit the content of the novel you want to write into text. So how do we write the novel in it? My friends don’t know, so let’s go to this site together. Let’s take some time to look at an introduction to how to write a novel. Share the Tomato novel tutorial on how to write a novel. 1. First open the Tomato free novel app on your mobile phone and click on Personal Center - Writer Center. 2. Jump to the Tomato Writer Assistant page - click on Create a new book at the end of the novel.

Publishing works on Douyin can attract more attention and likes, but sometimes it may be difficult for us to publish works in real time. In this case, we can use Douyin's scheduled release function. Douyin’s scheduled release function allows users to automatically publish works at a scheduled time, which can better plan the release plan and increase the exposure and influence of the work. 1. How to set the scheduled time for publishing works on Douyin? To set a scheduled release time, first go to Douyin's personal homepage, find the "+" button in the upper right corner, and click to enter the release page. There is a clock icon in the lower right corner of the publishing page. Click to enter the scheduled publishing interface. In the interface, you can choose the type of work you want to publish, including short videos, long videos, and live broadcasts. Next, you need to set a time for your work to be published. TikTok provides

Mobile games have become an integral part of people's lives with the development of technology. It has attracted the attention of many players with its cute dragon egg image and interesting hatching process, and one of the games that has attracted much attention is the mobile version of Dragon Egg. To help players better cultivate and grow their own dragons in the game, this article will introduce to you how to hatch dragon eggs in the mobile version. 1. Choose the appropriate type of dragon egg. Players need to carefully choose the type of dragon egg that they like and suit themselves, based on the different types of dragon egg attributes and abilities provided in the game. 2. Upgrade the level of the incubation machine. Players need to improve the level of the incubation machine by completing tasks and collecting props. The level of the incubation machine determines the hatching speed and hatching success rate. 3. Collect the resources required for hatching. Players need to be in the game

Setting font size has become an important personalization requirement as mobile phones become an important tool in people's daily lives. In order to meet the needs of different users, this article will introduce how to improve the mobile phone use experience and adjust the font size of the mobile phone through simple operations. Why do you need to adjust the font size of your mobile phone - Adjusting the font size can make the text clearer and easier to read - Suitable for the reading needs of users of different ages - Convenient for users with poor vision to use the font size setting function of the mobile phone system - How to enter the system settings interface - In Find and enter the "Display" option in the settings interface - find the "Font Size" option and adjust it. Adjust the font size with a third-party application - download and install an application that supports font size adjustment - open the application and enter the relevant settings interface - according to the individual

1. Open the Weibo client, click the three little dots on the editing page, and then click Scheduled Post. 2. After clicking on scheduled posting, there will be a time option on the right side of the publishing time. Set the time, edit the article, and click on the yellow words in the lower right corner to schedule posting. 3. The mobile version of Weibo does not currently support scheduled publishing. This function can only be used on the PC client!

As one of the most popular short video platforms in the world, Douyin allows everyone to become a creator and share every moment of life. For Douyin users, tags are a very important function. It can help users better classify and retrieve content, and also allows the platform to push appropriate content to users more accurately. So, where are the Douyin tags set? This article will explain in detail how to set up and use tags on Douyin. 1. Where is the Douyin tag set? Using tags on Douyin can help users better classify and label their works, making it easier for other users to find and follow them. The method to set the label is as follows: 1. Open the Douyin APP and log in to your account. 2. Click the "+" sign at the bottom of the screen and select the "Publish" button. 3.

Even answering calls in Do Not Disturb mode can be a very annoying experience. As the name suggests, Do Not Disturb mode turns off all incoming call notifications and alerts from emails, messages, etc. You can follow these solution sets to fix it. Fix 1 – Enable Focus Mode Enable focus mode on your phone. Step 1 – Swipe down from the top to access Control Center. Step 2 – Next, enable “Focus Mode” on your phone. Focus Mode enables Do Not Disturb mode on your phone. It won't cause any incoming call alerts to appear on your phone. Fix 2 – Change Focus Mode Settings If there are some issues in the focus mode settings, you should fix them. Step 1 – Open your iPhone settings window. Step 2 – Next, turn on the Focus mode settings
