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();
}
...
yii源码三 -- db的更多相关文章
- JFinal 源码分析 [DB+ActiveRecord]
我记得以前有人跟我说,“面试的时候要看spring的源码,要看ioc.aop的源码"那为什么要看这些开源框架的源码呢,其实很多人都是"应急式"的去读,就像读一篇文章一下, ...
- AQS源码三视-JUC系列
AQS源码三视-JUC系列 前两篇文章介绍了AQS的核心同步机制,使用CHL同步队列实现线程等待和唤醒,一个int值记录资源量.为上层各式各样的同步器实现画好了模版,像已经介绍到的ReentrantL ...
- Yii源码阅读笔记(三十三)
ServiceLocator,服务定位类,用于yii2中的依赖注入,通过以ID为索引的方式缓存服务或则组件的实例来定位服务或者组件: namespace yii\di; use Yii; use Cl ...
- Yii源码阅读笔记(一)
今天开始阅读yii2的源码,想深入了解一下yii框架的工作原理,同时学习一下优秀的编码规范和风格.在此记录一下阅读中的小心得. 每个框架都有一个入口文件,首先从入口文件开始,yii2的入口文件位于we ...
- JDK1.8源码(三)——java.lang.String 类
String 类也是java.lang 包下的一个类,算是日常编码中最常用的一个类了,那么本篇博客就来详细的介绍 String 类. 1.String 类的定义 public final class ...
- JDK1.8源码(三)——java.lang.String类
一.概述 1.介绍 String是一个final类,不可被继承,代表不可变的字符序列,是一个类类型的变量.Java程序中的所有字符串字面量(如"abc")都作为此类的实例实现,&q ...
- Unity UGUI图文混排源码(三) -- 动态表情
这里是根据图文混排源码(二)进一步修改的,其他链接也不贴了,就贴一个链接就好了,第一次看这文章的同学可以先去看看其他几篇文章 Unity UGUI图文混排源码(二):http://blog.csdn. ...
- spring事务源码分析结合mybatis源码(三)
下面将结合mybatis源码来分析下,这种持久化框架是如何对connection使用,来达到spring事务的控制. 想要在把mybatis跟spring整合都需要这样一个jar包:mybatis-s ...
- yii源码一 -- CComponent
CComponent: path:framework/base/CComponent.php overview:This file contains the foundation classes fo ...
随机推荐
- WebLogic使用总结(三)——WebLogic配置JNDI数据源
一.在WebLogic新建针对Oracle数据库的JNDI数据源 进入weblogic管理控制台,此处是远程访问:http://192.168.1.144:7001/console 点击左侧[ 域结构 ...
- MongoDB C# 驱动的各种版本下载地址
https://github.com/mongodb/mongo-csharp-driver/releases
- 委托、Lambda表达式、事件系列04,委托链是怎样形成的, 多播委托, 调用委托链方法,委托链异常处理
委托是多播委托,我们可以通过"+="把多个方法赋给委托变量,这样就形成了一个委托链.本篇的话题包括:委托链是怎样形成的,如何调用委托链方法,以及委托链异常处理. □ 调用返回类型为 ...
- 使用 Android 的日志工具LogCat
Android 中的日志工具类是 Log(android.util.Log),这个类中提供了如下几个方法来供我们打印日志. 1. Log.v() 这个方法用于打印那些最为琐碎的,意义最小的日志信 ...
- mycat应用场景
mycat应用场景 以下是几个典型的应用场景:单纯的读写分离,此时配置最为简单,支持读写分离,主从切换分表分库,对于超过1000万的表进行分片,最大支持1000亿的单表分片多租户应用,每个应用一个库, ...
- iOS objc_msgSend 报错解决方案
错误代码: objc_msgSend(self.beginRefreshingTaget, self.beginRefreshingAction, self); Too many arguments ...
- android 读取 raw 中的文件。
file.getParentFile().mkdir(); file.createNewFile(); InputStream inputStream = context.getResources() ...
- JAVA8 List排序
先定义一个实体类 @Data @AllArgsConstructor @NoArgsConstructor public class Human { private String name; priv ...
- linux中解压rar文件
linux平台默认是不支持RAR文件的解压,需要安装linux版本的RAR压缩软件,下载地址为:http://www.rarlab.com/download.htm 下载之后进行解压之后,进入rar目 ...
- Kubernetes基础
Kubernetes是什么 Kubernetes是当今最流行的开源容器管理平台,它就是大名鼎鼎的Google Borg的开源版本.Google在2014年推出了Kubernetes,本文发布时最新的版 ...