数据库之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
public function actionCreate () { //$user = new User; //实例化userModel //或 //$user = User::model(); //$user->setIsNewRecord(true); //给对应的字段赋值 $user ->username = "框架" ; $user ->status = 0 ; $user ->city = 5 ; //插入数据 //这里值得细说的是 IsNewRecord变量为true //场景(scenario)为insert //具体看 实现原理 if ( $user ->save()) { echo '插入成功' ; } else { var_dump( $user ->errors); } }C实现原理
实例化 User model
$user = new User;User extends CActiveRecord 调用 CActiveRecord的构造方法
public function __construct( $scenario = 'insert' ) { //使用 静态方法 model实例化对象 场景(scenario)为空 if ( $scenario === null ) // internally used by populateRecord() and model() return ; $this -> setScenario( $scenario ); //设置场景为 insert $this -> setIsNewRecord( true ); //设置 这个一条新纪录 //获得字段的默认值 $this -> _attributes = $this -> getMetaData() -> attributeDefaults; $this -> init(); //一个空方法 子类可以自己重写 $this -> attachBehaviors( $this -> behaviors()); //绑定行为 $this -> afterConstruct(); //触发 构造结束事件 }R
public function actionRead () { $user = User::model()->find(); //这里的场景(Scenario)仍然是update哦 $user = User::model()->find( 'id = :id' , array ( ':id' => 5 )); var_dump( $user ); } public function actionReadAll () { $user = User::model()->findAll(); $user = User::model()->findAll( 'id > :lid and id , array ( ':lid' => 5 , ':mid' => 10 )); var_dump( $user ); } public function actionReadCriteria () { $criteria = new CDbCriteria(); // $criteria->addCondition('id > :lid'); // $criteria->addCondition('id // $criteria->addBetweenCondition('id', 5, 10); //包含 5 和 10 // $criteria->addInCondition('id',array(4,5,6)); // $criteria->params = array(':lid'=>5,':mid'=>10); $criteria ->addSearchCondition( 'username' , 'g%' , false ); $criteria ->addSearchCondition( 'username' , 'g' ); $criteria ->order = 'id desc' ; // $criteria->limit = 2; // $criteria->offset = 1; $user = User::model()->findAll( $criteria ); var_dump( $user ); }R实现原理
find()和findall()
//创建一个 条件对象 CDbCriteria类 $criteria = $this ->getCommandBuilder() ->createCriteria( $condition , $params ); //查询 return $this ->query( $criteria , true );U
public function actionUpdate () { $id = Yii::app()->request->getParam( 'id' ); $user = User::model()->findByPk( $id ); $user ->username = "被我改了吧" ; //这里值得细说的是 IsNewRecord变量为false //场景(scenario)为update //具体看 实现原理 if ( $user ->save()) { echo '修改成功' ; } else { var_dump( $user ->errors); } }U实现原理
$user = User::model()->findByPk( $id );调用的User的 静态方法model
public static function model ( $className =__CLASS__) { return parent ::model( $className ); }调用父类 CActiveRecord 类的 model方法
public static function model ( $className =__CLASS__) { if ( isset ( self :: $_models [ $className ])) return self :: $_models [ $className ]; else { //实例化类 $model = self :: $_models [ $className ]= new $className ( null ); //绑定行为 $model ->attachBehaviors( $model ->behaviors()); return $model ; } }D
public function actionDelete () { $id = Yii::app()->request->getParam( 'id' ); $user = User::model()->findByPk( $id ); if ( $user ->delete()) { echo '删除成功' ; } else { var_dump( $user ->errors); } }D实现原理
没有什么特别的
if ( ! $this -> getIsNewRecord()) { Yii ::trace (get_class( $this ) . '.delete()' , 'system.db.ar.CActiveRecord' ); if ( $this -> beforeDelete()) { $result = $this -> deleteByPk( $this -> getPrimaryKey()) > 0 ; $this -> afterDelete(); return $result ; } else return false ; }场景和新纪录
场景(scenario)
新纪录(IsNewRecord)1.场景的作用更多体现在 insert 和 update上,这也是默认的场景只有insert和update
new User() 场景被赋值成insert
User::model() 在 query() 的时候 调用 populateRecords() 赋值成update节选自yiichina网友的一片博文,比较好的描述了yii中场景的作用
2.新纪录一般用来区别insert和其他操作
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did95999