数据库之AR
数据库之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和其他操作

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

Go language is an efficient, concise and easy-to-learn programming language. It is favored by developers because of its advantages in concurrent programming and network programming. In actual development, database operations are an indispensable part. This article will introduce how to use Go language to implement database addition, deletion, modification and query operations. In Go language, we usually use third-party libraries to operate databases, such as commonly used sql packages, gorm, etc. Here we take the sql package as an example to introduce how to implement the addition, deletion, modification and query operations of the database. Assume we are using a MySQL database.

Here's how to convert a MySQL query result array into an object: Create an empty object array. Loop through the resulting array and create a new object for each row. Use a foreach loop to assign the key-value pairs of each row to the corresponding properties of the new object. Adds a new object to the object array. Close the database connection.

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

Hibernate polymorphic mapping can map inherited classes to the database and provides the following mapping types: joined-subclass: Create a separate table for the subclass, including all columns of the parent class. table-per-class: Create a separate table for subclasses, containing only subclass-specific columns. union-subclass: similar to joined-subclass, but the parent class table unions all subclass columns.

In PHP, an array is an ordered sequence, and elements are accessed by index; an object is an entity with properties and methods, created through the new keyword. Array access is via index, object access is via properties/methods. Array values are passed and object references are passed.

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

In C++, there are three points to note when a function returns an object: The life cycle of the object is managed by the caller to prevent memory leaks. Avoid dangling pointers and ensure the object remains valid after the function returns by dynamically allocating memory or returning the object itself. The compiler may optimize copy generation of the returned object to improve performance, but if the object is passed by value semantics, no copy generation is required.
