数据库之AR Active Record (AR) 是一个流行的 对象-关系映射 (ORM) 技术。 每个 AR 类代表一个数据表(或视图),数据表(或视图)的列在 AR 类中体现为类的属性,一个 AR 实例则表示表中的一行。 – yiichina 数据库之AR gii CRUD C C实现原理 R R实现原理
数据库之AR
Active Record (AR) 是一个流行的 对象-关系映射 (ORM) 技术。 每个 AR 类代表一个数据表(或视图),数据表(或视图)的列在 AR 类中体现为类的属性,一个 AR 实例则表示表中的一行。
– yiichina
- 数据库之AR
- gii
- CRUD
- C
- C实现原理
- R
- R实现原理
- U
- U实现原理
- D
- D实现原理
- 场景和新纪录
gii
这里简单提一下gii 具体百度一下,你就知道
是YII的代码生成工具
下面使用的User类就是gii生成的
CRUD
C
<code><span>public</span> <span><span>function</span> <span>actionCreate</span><span>()</span> {</span> <span>//$user = new User; //实例化userModel</span> <span>//或</span> <span>//$user = User::model();</span> <span>//$user->setIsNewRecord(true);</span> <span>//给对应的字段赋值</span> <span>$user</span>->username = <span>"框架"</span>; <span>$user</span>->status = <span>0</span>; <span>$user</span>->city = <span>5</span>; <span>//插入数据</span> <span>//这里值得细说的是 IsNewRecord变量为true</span> <span>//场景(scenario)为insert </span> <span>//具体看 实现原理</span> <span>if</span>(<span>$user</span>->save()) { <span>echo</span> <span>'插入成功'</span>; }<span>else</span> { var_dump(<span>$user</span>->errors); } }</code>
C实现原理
实例化 User model
<code><span>$user</span> = <span>new</span> User;</code>
User extends CActiveRecord 调用 CActiveRecord的构造方法
<code><span>public</span> function __construct(<span>$scenario</span><span>=</span><span>'insert'</span>) { <span>//使用 静态方法 model实例化对象 场景(scenario)为空</span> <span>if</span>(<span>$scenario</span><span>===</span><span>null</span>) <span>// internally used by populateRecord() and model()</span> <span>return</span>; <span>$this</span><span>-></span>setScenario(<span>$scenario</span>); <span>//设置场景为 insert</span> <span>$this</span><span>-></span>setIsNewRecord(<span>true</span>); <span>//设置 这个一条新纪录</span> <span>//获得字段的默认值</span> <span>$this</span><span>-></span>_attributes<span>=</span><span>$this</span><span>-></span>getMetaData()<span>-></span>attributeDefaults; <span>$this</span><span>-></span>init(); <span>//一个空方法 子类可以自己重写</span> <span>$this</span><span>-></span>attachBehaviors(<span>$this</span><span>-></span>behaviors()); <span>//绑定行为</span> <span>$this</span><span>-></span>afterConstruct(); <span>//触发 构造结束事件</span> }</code>
R
<code><span>public</span> <span><span>function</span> <span>actionRead</span><span>()</span> {</span> <span>$user</span> = User::model()->find(); <span>//这里的场景(Scenario)仍然是update哦</span> <span>$user</span> = User::model()->find(<span>'id = :id'</span>,<span>array</span>(<span>':id'</span>=><span>5</span>)); var_dump(<span>$user</span>); } <span>public</span> <span><span>function</span> <span>actionReadAll</span><span>()</span> {</span> <span>$user</span> = User::model()->findAll(); <span>$user</span> = User::model()->findAll(<span>'id > :lid and id ,<span>array</span>(<span>':lid'</span>=><span>5</span>,<span>':mid'</span>=><span>10</span>)); var_dump(<span>$user</span>); } <span>public</span> <span><span>function</span> <span>actionReadCriteria</span><span>()</span> {</span> <span>$criteria</span> = <span>new</span> CDbCriteria(); <span>// $criteria->addCondition('id > :lid');</span> <span>// $criteria->addCondition('id <span>// $criteria->addBetweenCondition('id', 5, 10); //包含 5 和 10</span> <span>// $criteria->addInCondition('id',array(4,5,6)); </span> <span>// $criteria->params = array(':lid'=>5,':mid'=>10);</span> <span>$criteria</span>->addSearchCondition(<span>'username'</span>, <span>'g%'</span> ,<span>false</span>); <span>$criteria</span>->addSearchCondition(<span>'username'</span>, <span>'g'</span>); <span>$criteria</span>->order = <span>'id desc'</span>; <span>// $criteria->limit = 2;</span> <span>// $criteria->offset = 1;</span> <span>$user</span> = User::model()->findAll(<span>$criteria</span>); var_dump(<span>$user</span>); }</span></span></code>
R实现原理
find()和findall()
<code><span>//创建一个 条件对象 CDbCriteria类</span> <span>$criteria</span>=<span>$this</span>->getCommandBuilder() ->createCriteria(<span>$condition</span>,<span>$params</span>); <span>//查询</span> <span>return</span> <span>$this</span>->query(<span>$criteria</span>,<span>true</span>);</code>
U
<code><span>public</span> <span><span>function</span> <span>actionUpdate</span><span>()</span> {</span> <span>$id</span> = Yii::app()->request->getParam(<span>'id'</span>); <span>$user</span> = User::model()->findByPk(<span>$id</span>); <span>$user</span>->username = <span>"被我改了吧"</span>; <span>//这里值得细说的是 IsNewRecord变量为false</span> <span>//场景(scenario)为update</span> <span>//具体看 实现原理</span> <span>if</span>(<span>$user</span>->save()) { <span>echo</span> <span>'修改成功'</span>; }<span>else</span> { var_dump(<span>$user</span>->errors); } }</code>
U实现原理
<code><span>$user</span> = User::model()->findByPk(<span>$id</span>);</code>
调用的User的 静态方法model
<code><span>public</span> <span>static</span> <span><span>function</span> <span>model</span><span>(<span>$className</span>=__CLASS__)</span> {</span> <span>return</span> <span>parent</span>::model(<span>$className</span>); }</code>
调用父类 CActiveRecord 类的 model方法
<code><span>public</span> <span>static</span> <span><span>function</span> <span>model</span><span>(<span>$className</span>=__CLASS__)</span> {</span> <span>if</span>(<span>isset</span>(<span>self</span>::<span>$_models</span>[<span>$className</span>])) <span>return</span> <span>self</span>::<span>$_models</span>[<span>$className</span>]; <span>else</span> { <span>//实例化类</span> <span>$model</span>=<span>self</span>::<span>$_models</span>[<span>$className</span>]=<span>new</span> <span>$className</span>(<span>null</span>); <span>//绑定行为</span> <span>$model</span>->attachBehaviors(<span>$model</span>->behaviors()); <span>return</span> <span>$model</span>; } }</code>
D
<code><span>public</span> <span><span>function</span> <span>actionDelete</span><span>()</span> {</span> <span>$id</span> = Yii::app()->request->getParam(<span>'id'</span>); <span>$user</span> = User::model()->findByPk(<span>$id</span>); <span>if</span>(<span>$user</span>->delete()) { <span>echo</span> <span>'删除成功'</span>; } <span>else</span> { var_dump(<span>$user</span>->errors); } }</code>
D实现原理
没有什么特别的
<code><span>if</span>(<span>!</span><span>$this</span><span>-></span>getIsNewRecord()) { Yii<span>::trace</span>(get_class(<span>$this</span>)<span>.</span><span>'.delete()'</span>,<span>'system.db.ar.CActiveRecord'</span>); <span>if</span>(<span>$this</span><span>-></span>beforeDelete()) { <span>$result</span><span>=</span><span>$this</span><span>-></span>deleteByPk(<span>$this</span><span>-></span>getPrimaryKey())<span>></span><span>0</span>; <span>$this</span><span>-></span>afterDelete(); <span>return</span> <span>$result</span>; } <span>else</span> <span>return</span> <span>false</span>; }</code>
场景和新纪录
场景(scenario)
新纪录(IsNewRecord)
1.场景的作用更多体现在 insert 和 update上,这也是默认的场景只有insert和update
new User()
场景被赋值成insert
User::model()
在 query()
的时候 调用 populateRecords()
赋值成update
节选自yiichina网友的一片博文,比较好的描述了yii中场景的作用
2.新纪录一般用来区别insert和其他操作

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


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

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software