Maison >base de données >tutoriel mysql >数据库之AR
数据库之AR Active Record (AR) 是一个流行的 对象-关系映射 (ORM) 技术。 每个 AR 类代表一个数据表(或视图),数据表(或视图)的列在 AR 类中体现为类的属性,一个 AR 实例则表示表中的一行。 – yiichina 数据库之AR gii CRUD C C实现原理 R R实现原理
Active Record (AR) 是一个流行的 对象-关系映射 (ORM) 技术。 每个 AR 类代表一个数据表(或视图),数据表(或视图)的列在 AR 类中体现为类的属性,一个 AR 实例则表示表中的一行。
– yiichina
这里简单提一下gii 具体百度一下,你就知道
是YII的代码生成工具
下面使用的User类就是gii生成的
<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>
实例化 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>
<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>
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>
<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>
<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>
<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>
没有什么特别的
<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和其他操作