yii2.0数据库交互ORM

ORM Object  Relational  Mapping  对象关系映射

ObjectPHP 对象,属性

Relational 数据库表,字段

Mapping 对象和表的关系;属性与字段的关系 

自制ORM框架

在models文件夹下创建AOrm基类 AOrm.php

namespace  app\models;
abstract class AOrm{
    public $tablename;
    public $primaryKey='id';
    public $id;

    public abstract function Mapping();
    /*
     *  将对象持久化到数据库
     *  如果id属性没有被赋值,那么执行插入操作,否则执行更新操作
     */
    public  function  save(){


                 $sql=$this->buildSaveSql();
                 \yii::$app->db->createCommand($sql)->execute();
    }

    private function buildSaveSql(){
        $sql='';
        $mapping=$this->Mapping();
        $fields=[];
        $values=[];
        if(empty($this->id)){
            /*
                * 没有id执行插入操作
                * INSERT INTO table_name() values()
            */
            foreach($mapping as $k=>$v){
                if(isset($this->$k)){
                /*
                 * mapping中的$k有uName,       pwd,        nName,      restMoney,
                 * mapping中的$v有username,    password,   nickname,   rest
                 * 所以$this->$k指向的是
                 *                      public $uName
                 *                      public $pwd
                 *                      public $nName
                 *                      public $restMoney
                 * isset($this->$k)表示这些变量是否有值;
                 * $this->$k需要在控制器中赋值,如: AomController下
                 *          $object=new BUser()
                 *          $object->nName='AORM';
                 */
                    array_push($fields,$v);
                    array_push($values,"'{$this->$k}'");

                }
            }
                 return $sql='INSERT INTO '.$this->tablename.'('.implode(',',$fields).') VALUES('.implode(',',$values).')';
        }else{
            $set=[];
                //更新操作 UPDATE table_name SET  field=value , field=value where id=
            foreach($mapping as $k=>$v){
                if(isset($this->$k)){
                    array_push($set, "{$v}='{$this->$k}'");
                 }
             }
            return $sql='UPDATE '.$this->tablename.' SET '.implode(',',$set).' WHERE '.$this->primaryKey.'='.$this->id;
        }

    }
        public function del(){
            if(!empty($this->id)){
                \yii::$app->db->createCommand('DELETE FROM '.$this->tablename.' where '.$this->primaryKey.'='.$this->id)->execute();
            }else{
                return false;
            }
         }


}
创建子类文件models/BUser.php

 
namespace  app\models;
class BUser extends AOrm{
    public $tablename='test';
    public $id;
    public $uName;
    public $pwd;
    public $nName;
    public $restMoney;

    public function Mapping()
    {
        // TODO: Implement Mapping() method.
        return [
            'uName'     =>'username',
            'pwd'       =>'password',
            'nName'     =>'nickname',
            'restMoney' =>'rest'
        ];
    }

}
在控制器中调用models/AOrm下的方法

 
namespace  app\controllers;
use app\models\BUser;
use yii\web\Controller;

class AomController extends Controller{

    public function actionTest(){
         $object= new BUser() ;
         $object->uName='ORM';
         $object->pwd='123456';
         $object->nName='哈哈';
         $object->id=4;
         $res=$object->del();
         echo "
";
            print_r($res);
    }
}

你可能感兴趣的:(Yii2.0初学)