yii model层操作总结
yii model层操作总结
yii model层操作属性和方法总结。
tableName – 设置Model所对应的表名,例如:
public function tableName(){return 'gshop_order_ext';}
rules – 设置Model里各字段的验证规则
relations – 设置关联规则
attributeLabels – 设置各字段的别名
safeAttributes – 设置可以修改属性的字段
beforeValidate和afterValidate – 字段验证前和验证后执行的函数, 需要返回true值
beforeSave和afterSave – 记录存储前和存储后执行的函数, 需要返回true值
其次, Yii里的ORM使用的是AR, 有几个主要的操作, 分别是:
save – 操作数据
update – 修改数据
delete – 删除数据
validate – 验证数据
读取记录时, 有以下几个方法:
findByPk – 通过主键查找记录, 结果为单条记录
findByAttribute – 通过属性查找记录, 结果为单条记录
findAllByAttributes – 通过属性查找数据, 结果为记录集
findAll – 通过CDbCriteria对象查找数据, 结果为记录集 *
查找方法接收的参数有2种, 没有星号的, 接受数组作为参数, 有星号的接收CDbCriteria对象作为参数, 使用对象的时候, 可以提供更多的查找条件, 下面给出一个实例:
$criteria = new CDbCriteria; // 创建CDbCriteria对象
$criteria->condition = 'title LIKE %' . 'php' . '%'; // 设置查询条件
$criteria->order = 'createdTime DESC'; // 设置排序条件
$criteria->limit = 10; // 限定记录的条数
$criteria->select = 'id,title,content'; // 设置结果所包含的字段
$articles = Article::model()->findAll($criteria); //结果为数组, 其中每个元素为记录对象
再次, Yii对关联数据默认采用LazyLoad的加载形式, 即需要时才读取. 这样, 当我们不需要关联数据的时候, Yii是不会帮我们去读取的, 大大加快了反应的速度. 但是也有一些时候, 我们是需要关联数据的, 比如说读取文章的时候, 我们是需要文章所属的分类的, 如果使用LazyLoad的形式, 就要有多少条, 查询多少次, 效率非常低下, 这时就需要EagerLoad了, 即一次把关联表的数据全部读出.
比如:
$articles = Article::model()->with('category')->findAll();
使用with将关联表的数据一次全部读出. 关联表的设置在Model里的relation里设置.
比如:
public function relations() { return array( 'category' => array(self::BELONGS_TO, 'Category', 'categoryId'), ); }
非常的清晰明了。
您可能感兴趣的文章
- Yii framework框架之模块开发分析
- Yii框架Yiiapp()的理解
- yii 数据库添加,修改,删除相关操作总结
- yii数据库查询操作总结
- Yii CDbCriteria的常用方法总结
- Yii 获取当前控制器名称和动作名称
- Yii使用PHPExcel导入Excel文件的方法
- Yii 动作方法技巧

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

AI Hentai Generator
Generate AI Hentai for free.

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

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

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.

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.

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

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

1. First enter Weibo, then click on me in the lower right corner and select [Customer Service]. 2. Then enter [Watermark] in the search box and select [Set Weibo Image Watermark]. 3. Then click [Link] in the interface. 4. Then click [Image Watermark Settings] in the newly opened window. 5. Finally, check [Picture Center] and click [Save].
