简介:
Yii DAO 主要包含如下四个类:
CDbConnection: 代表一个数据库连接。
CDbCommand: 代表一条通过数据库执行的 SQL 语句。
CDbDataReader: 代表一个只向前移动的,来自一个查询结果集中的行的流。
CDbTransaction: 代表一个数据库事务。
#: 执行一个无查询语句execute(): INSERT, UPDATE 和 DELETE 。如果成功,它将返回此执行所影响的行数。
#:执行插入(单条)
$sql = "INSERT INTO test(username,sex) VALUES('张三',1)";
$command = Yii::app()->db_product->createCommand($sql);
$data = $command->execute(); #成功返回1
#:执行单条插入(占位符形式)
$sql = "INSERT INTO test(username,sex) VALUES (:username,:sex)"; #注意占位符不需要引号
$data = Yii::app()->db_product->createCommand($sql)->execute([':username'=>'he',':sex'=>2]);
#:插入多条(原始语句)
$sql = "INSERT INTO test(username,sex,add_time) VALUES ('李思',2,NOW() ),('王五',1,NOW())";
$command = Yii::app()->db_product->createCommand($sql);
$data = $command->execute(); #成功返回1
#插入多条(构造占位符形式)
$sql = "INSERT INTO test(username,sex) VALUES (:username1,:sex1),(:username2,:sex2)";
$data = Yii::app()->db_product->createCommand($sql)
->execute([':username1'=>'he1',':sex1'=>1,':username2'=>'he2',':sex2'=>2]);
#插入多条(构造原始语句)
$params = [
[
'username' => '新增1',
'sex' => 1,
'add_time' => date('Y-m-d H:i:s')
],
[
'username' => '新增2',
'sex' => 1,
'add_time' => date('Y-m-d H:i:s')
]
];
$rows = [];
foreach ($params as $k=>$row){
$rows[] = "('".implode("','",$row)."')"; #需要完善,当值带有双引号或单引号的问题
}
$cloums = array_keys(reset($params)); #构造插入表字段
$sql = 'insert into test';
$sql .= ' ('.implode(',',$cloums).') values '.implode(',',$rows);
#insert into test (username,sex,add_time) values ('新增1','1','2018-08-31 13:54:07'),('新增2','1','2018-08-31 13:54:07')
$data = Yii::app()->db_product->createCommand($sql)->execute();
#插入多条(构造占位符形式)
$placeHolders = [];
$placeParams = [];
$i=1;
foreach ($params as $rows){
$placeHolder = [];
foreach ($rows as $kK=>$value){
$placeHolder[] = ':'.$kK.$i;
$placeParams[':'.$kK.$i] = $value; #构造占位符对应的值
}
$placeHolders[] = "(".implode(',',$placeHolder).")"; #构造占位符及sql语句
$i++;
unset($placeHolder);
}
$cloums = array_keys(reset($params));
$sql = 'insert into test';
$sql .= ' ('.implode(',',$cloums).') values '.implode(',',$placeHolders);
$data = Yii::app()->db_product->createCommand($sql)->execute($placeParams);
#:修改
$sql = "update test set username='修改2',sex=2,update_time=now() WHERE id = 2";
$command = Yii::app()->db_product->createCommand($sql);
$data = $command->execute(); #成功返回1
#批量更新:原理语句
// UPDATE test
// SET username = CASE id
// WHEN 1 THEN 3
// WHEN 2 THEN 4
// WHEN 3 THEN 5
// END,
// title = CASE id
// WHEN 1 THEN 'New Title 1'
// WHEN 2 THEN 'New Title 2'
// WHEN 3 THEN 'New Title 3'
// END
// WHERE id IN (1,2,3)
#代码实现:
#批量修改(单个字段)
$params = [
'1'=>'赵一"',
'2'=>'钱二',
'3'=>'张"三',
'4'=>"李'四",
'5'=>'王五',
'6'=>'刘六'
];
$ids = implode(',', array_keys($params));
$sql = "UPDATE test SET username = CASE id ";
foreach ($params as $id => $ordinal) {
$sql .= sprintf("WHEN %d THEN '%s' ", $id, addslashes($ordinal));
}
$sql .= "END WHERE id IN ($ids)";
$data = Yii::app()->db_product->createCommand($sql)->execute();
#:删除
$sql = "delete from test where id = 1";
$command = Yii::app()->db_product->createCommand($sql);
$data = $command->execute(); #成功返回1
2、查询
#1.query
$sql = 'select * from test';
$command = Yii::app()->db_product->createCommand($sql);
$dataReader = $command->query();
#1.1 重复调用 read() 直到它返回 false
while(($row=$dataReader->read())!==false) {
$data[] = $row;
}
#1.2 使用 foreach 遍历数据中的每一行
foreach($dataReader as $row) {
$data[] = $row;
}
#1.3 一次性提取所有行到一个数组
$data = $dataReader->readAll();
3、queryXXX
#2.查询并返回结果中的所有行:queryAll()
$sql = 'select * from test';
$data = Yii::app()->db_product->createCommand($sql)->queryAll();
#3.查询并返回结果中的第一行
$sql = 'select * from test';
$data = Yii::app()->db_product->createCommand($sql)->queryRow();
#4.查询并返回结果中的第一列
$sql = 'select * from test';
$data = Yii::app()->db_product->createCommand($sql)->queryColumn();
#5.查询并返回结果中第一行的第一个字段
$sql = 'select * from test';
$data = Yii::app()->db_product->createCommand($sql)->queryScalar();
4、绑定参数
#6.绑定参数
$username = '刘老三';
$sex = 1;
$sql="INSERT INTO test (username,sex) VALUES (:username,:sex)";
$command = Yii::app()->db_product->createCommand($sql);
$command->bindParam(':username',$username,PDO::PARAM_STR); #绑定参数
$command->bindParam(":sex",$sex,PDO::PARAM_INT);
$data = $command->execute(); #成功返回1
$id = 1;
$sql = "select * from test where id=:id";
$data = Yii::app()->db_product->createCommand($sql)->bindParam(":id",$id,\PDO::PARAM_INT)->queryAll();
#6.2:批量绑定 ??
5、绑定值
#绑定值
$sql="INSERT INTO test (username,sex) VALUES (:username,:sex)";
$command = Yii::app()->db_product->createCommand($sql);
$command->bindValue(':username','hao',PDO::PARAM_STR);
$command->bindValue(":sex",2,PDO::PARAM_INT);
$data = $command->execute();
$sql = "select * from test where id=:id";
$data = Yii::app()->db_product->createCommand($sql)->bindValue(":id",2,\PDO::PARAM_INT)->queryAll();