• yii源码三 -- db


    《AR》

    CActiveRecord:
    path:/framework/db/ar/CActiveRecord.php
    overview:is the base class for classes representing relational data.It implements the active record design pattern, a popular Object-Relational Mapping (ORM) technique.

    实现原理:首先一套__sleep(),__get(),__set(),__isset(),__unset(),__call()魔术方法,来处理model和model属性。

    public function getRelated($name,$refresh=false,$params=array()){}
      对该方法,有一段很重要的介绍如下:

    * Returns the related record(s).
    * This method will return the related record(s) of the current record.
    * If the relation is HAS_ONE or BELONGS_TO, it will return a single object
    * or null if the object does not exist.
    * If the relation is HAS_MANY or MANY_MANY, it will return an array of objects
    * or an empty array.

    public function primaryKey(){}
      如何表设置了主键,则返回null,不需要覆盖该方法;否则返回array(),且需要覆盖该方法。

    《schema》

    CDbConnection:
    path:/framework/db/CDbConnection.php
    overview:CDbConnection represents a connection to a database.

    工作原理:CDbConnection works together with CDbCommand, CDbDataReader and CDbTransaction to provide data access to various DBMS。且基于PDO扩展。

      首先用$connection = new CDbConnection($dsn,$username,$password)创建一个连接实例,然后调用$connection->active=true启动连接。

    用法:
    1.创建DB实例并建立连接:

    $connection=new CDbConnection($dsn,$username,$password);
    $connection->active=true; //建立连接

    2.执行sql语句:

    $command=$connection->createCommand($sqlStatement);
    $command->execute();   //执行非查询语句(insert,delete,update)
    $reader=$command->query();//执行查询语句(select)

    备注:预处理和绑定参数用法:

    $command=$connection->createCommand($sqlStatement);
    $command->bindParam($name1,$value1);
    $command->bindParam($name2,$value2);
    $command->execute();

    3.获取结果:

    foreach($reader as $row){
          ......  
    }

    4.事务:

    $transaction=$connection->beginTransaction();
    try{
        $connection->createCommand($sql1)->execute();
        $connection->createCommand($sql2)->execute();
        //.... other SQL executions
        $transaction->commit();
    }catch(Exception $e){
        $transaction->rollback();
     }

    5.由于CDbConnection实现了IApplicationComponent接口,所以它可以当作一个application component并且在application配置文件(main.php)里配置数据库连接信息:

    array(
         'components'=>array(
             'db'=>array(
                  'class'=>'CDbConnection',
                  'connectionString'=>'sqlite:path/to/dbfile',
             ),
         ),
    )

    CDbCommandBuilder:
    path:/framework/db/schema/CDbCommandBuilder.php
    overview:provides basic methods to create query commands for tables.

    CDbDataReader:
    path:/framework/db/schema/CDbDataReader.php
    overview:represents a forward-only stream of rows from a query result set.

    CDbTransaction:
    path:/framework/db/schema/CDbTransaction.php
    overview:a DB transaction.

    $transaction=$connection->beginTransaction();
    try{
        $connection->createCommand($sql1)->execute();
        $connection->createCommand($sql2)->execute();
        //.... other SQL executions
        $transaction->commit();
    }
    catch(Exception $e){
        $transaction->rollback();
    }

    ...

  • 相关阅读:
    Burpsuite intruder模块 越过token进行爆破,包含靶场搭建
    burpsuiteb windows10 下载与安装
    sqlmap的命令总结
    Vue.js与jQuery混用
    IE低版本cors跨域请求
    window.open打开网址被拦截
    一图一知之python3数据类型
    一图一知-vue强大的slot
    一图一知-强大的js数组
    windows中git输错密码后不能修改问题
  • 原文地址:https://www.cnblogs.com/thinksasa/p/3853341.html
Copyright © 2020-2023  润新知