[size=large]
1 .Select: 常用几种方法
// find the first row satisfying the specified condition
$post=Post::model()->find($condition,$params);
// find the row with the specified primary key
$post=Post::model()->findByPk($postID,$condition,$params);
// find the row with the specified attribute values
$post=Post::model()->findByAttributes($attributes,$condition,$params);
// find the first row using the specified SQL statement
$post=Post::model()->findBySql($sql,$params);
$criteria=new CDbCriteria;
$criteria->select='title'; // only select the 'title' column
$criteria->condition='postID=:postID';
$criteria->params=array(':postID'=>10);
$post=Post::model()->find($criteria);
$post=Post::model()->find(array(
'select'=>'title',
'condition'=>'postID=:postID',
'params'=>array(':postID'=>10),
));
// find all rows satisfying the specified condition
$posts=Post::model()->findAll($condition,$params);
// find all rows with the specified primary keys
$posts=Post::model()->findAllByPk($postIDs,$condition,$params);
// find all rows with the specified attribute values
$posts=Post::model()->findAllByAttributes($attributes,$condition,$params);
// find all rows using the specified SQL statement
$posts=Post::model()->findAllBySql($sql,$params);
// get the number of rows satisfying the specified condition
$n=Post::model()->count($condition,$params);
// get the number of rows using the specified SQL statement
$n=Post::model()->countBySql($sql,$params);
// check if there is at least a row satisfying the specified condition
$exists=Post::model()->exists($condition,$params);
2.常用的修改方式--update
// update the rows matching the specified condition
Post::model()->updateAll($attributes,$condition,$params);
// update the rows matching the specified condition and primary key(s)
Post::model()->updateByPk($pk,$attributes,$condition,$params);
// update counter columns in the rows satisfying the specified conditions
Post::model()->updateCounters($counters,$condition,$params);
3.常用的删除方式--delete
$post=Post::model()->findByPk(10); // assuming there is a post whose ID is 10
$post->delete();
// delete the rows matching the specified condition
Post::model()->deleteAll($condition,$params);
// delete the rows matching the specified condition and primary key(s)
Post::model()->deleteByPk($pk,$condition,$params);
4.常用的事物--innodb
$model=Post::model();
$transaction=$model->dbConnection->beginTransaction();
try
{
// find and save are two steps which may be intervened by another request
// we therefore use a transaction to ensure consistency and integrity
$post=$model->findByPk(10);
$post->title='new post title';
$post->save();
$transaction->commit();
}
catch(Exception $e)
{
$transaction->rollBack();
}
希望对你们有帮助![/size]
分享到:
相关推荐
yii对象操作、直接数据库操作、事物等操作。几乎所有的操作都收录了
php YII框架数据库备份模块
本文实例讲述了Yii实现多数据库主从读写分离的方法。分享给大家供大家参考。具体分析如下: Yii框架数据库多数据库、主从、读写分离 实现,功能描述: 1.实现主从数据库读写分离 主库:写 从库(可多个):读 2.主数据库...
Yii框架数据库操作分享ppt (Mac版,windows需转换格式)
YII 2数据库迁移(Migrations) 使用教程,使用migrate功能迁移数据库
本文实例讲述了Yii 框架使用数据库(databases)的方法。分享给大家供大家参考,具体如下: 配置数据库连接 ¶ 开始之前,请确保你已经安装了 PHP PDO 扩展和你所使用的数据库的 PDO 驱动(例如 MySQL 的 pdo_mysql)...
这个是关于yii2常用的笔记
主要介绍了YII2框架中操作数据库的方式,结合实例形式总结分析了YII2使用createCommand方式及AR(Active Record)方式操作数据库相关实现技巧,需要的朋友可以参考下
本文实例讲述了Yii框架 session 数据库存储操作方法。分享给大家供大家参考,具体如下: 在组件中声明session 组件开启数据库库存储 [ 'components' => [ 'db' => [ 'class' => 'yii\db\Connection', 'dsn' => ...
本篇文章主要介绍了Yii2框架实现数据库常用操作总结,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
yii DAO编程yii DAO编程yii DAO编程yii DAO编程yii DAO编程yii DAO编程yii DAO编程yii DAO编程
User::find()->all(); //返回所有用户数据; User::findOne($id); //返回 主键 id=1 的一条数据; User::find()->where([‘name’ => ‘ttt’])->one(); //返回 [‘name’ => ‘ttt’] 的一条数据;...
Yii2备份和还原数据库 数据库备份和还原功能 安装 安装此扩展的首选方法是通过 。 无论运行 php composer.phar require --prefer-dist spanjeta/yii2-backup "*" 或添加 "spanjeta/yii2-backup": "*" 到composer....
本文讲述了yii2.0数据库迁移的方法。分享给大家供大家参考,具体如下: 创建迁移 使用如下命令来创建一个新的迁移: yii migrate/create 必填参数 name 的作用是对新的迁移做一个简要的描述。例如,如果这个迁移...
PHP框架Yii商城源码,带数据库文件.rar
从数据库管理配置 安装 一 安装此扩展的首选方法是通过 。 要么跑 php composer.phar require --prefer-dist sersid/yii2-config " * " 或添加 "sersid/yii2-config": "*" 到composer.json文件的 require 部分。 ...
主要介绍了Yii2数据库操作常用方法,结合实例形式总结分析了Yii2常用的增删查改及配置相关操作技巧,需要的朋友可以参考下
yii2-schemadump 从现有数据库生成模式。演示版要求PHP 7.3或更高版本Yii 2.x安装composer require --dev jamband/yii2-schemadump用法在config / console.php中添加以下内容: return [ . . . 'components' => [ . ...