当前位置: 移动技术网 > IT编程>开发语言>PHP > Yii2数据库操作常用方法小结

Yii2数据库操作常用方法小结

2017年12月12日  | 移动技术网IT编程  | 我要评论

本文实例讲述了yii2数据库操作常用方法。分享给大家供大家参考,具体如下:

查询:

// find the customers whose primary key value is 10
$customers = customer::findall(10);
$customer = customer::findone(10);
// the above code is equivalent to:
$customers = customer::find()->where(['id' => 10])->all();
// find the customers whose primary key value is 10, 11 or 12.
$customers = customer::findall([10, 11, 12]);
$customers = customer::find()->where(['in','id',[10,11,12]])->all();
// the above code is equivalent to:
$customers = customer::find()->where(['id' => [10, 11, 12]])->all();
// find customers whose age is 30 and whose status is 1
$customers = customer::findall(['age' => 30, 'status' => 1]);
// the above code is equivalent to:
$customers = customer::find()->where(['age' => 30, 'status' => 1])->all();
// use params binding
$customers = customer::find()->where('age=:age and status=:status')->addparams([':age'=>30,':status'=>1])->all();
// use index
$customers = customer::find()->indexby('id')->where(['age' => 30, 'status' => 1])->all();
// get customers count
$count = customer::find()->where(['age' => 30, 'status' => 1])->count();
// add addition condition
$customers = customer::find()->where(['age' => 30, 'status' => 1])->andwhere('score > 100')->orderby('id desc')->offset(5)->limit(10)->all();
// find by sql
$customers = customer::findbysql('select * from customer where age=30 and status=1 and score>100 order by id desc limit 5,10')->all();

修改:

// update status for customer-10
$customer = customer::findone(10);
$customer->status = 1;
$customer->update();
// the above code is equivalent to:
customer::updateall(['status' => 1], 'id = :id',[':id'=>10]);

删除:

// delete customer-10
customer::findone(10)->delete();
// the above code is equivalent to:
customer::deleteall(['status' => 1], 'id = :id',[':id'=>10]);

----------------使用子查询----------------------

$subquery = (new query())->select('count(*)')->from('customer');
// select `id`, (select count(*) from `customer`) as `count` from `customer`
$query = (new query())->select(['id', 'count' => $subquery])->from('customer');

----------------手写sql-----------------------

// select
$customers = yii::$app->db->createcommand('select * from customer')->queryall();
// update
yii::$app->db->createcommand()->update('customer',['status'=>1],'id=10')->execute();
// delete
yii::$app->db->createcommand()->delete('customer','id=10')->execute();
//transaction
// outer
$transaction1 = $connection->begintransaction();
try {
  $connection->createcommand($sql1)->execute();
  // internal
  $transaction2 = $connection->begintransaction();
  try {
    $connection->createcommand($sql2)->execute();
    $transaction2->commit();
  } catch (exception $e) {
    $transaction2->rollback();
  }
  $transaction1->commit();
} catch (exception $e) {
  $transaction1->rollback();
}

---------------主从配置----------------------

[
  'class' => 'yii\db\connection',
  // master
  'dsn' => 'dsn for master server',
  'username' => 'master',
  'password' => '',
  // slaves
  'slaveconfig' => [
    'username' => 'slave',
    'password' => '',
    'attributes' => [
      // use a smaller connection timeout
      pdo::attr_timeout => 10,
    ],
  ],
  'slaves' => [
    ['dsn' => 'dsn for slave server 1'],
    ['dsn' => 'dsn for slave server 2'],
    ['dsn' => 'dsn for slave server 3'],
    ['dsn' => 'dsn for slave server 4'],
  ],
]

更多关于yii相关内容感兴趣的读者可查看本站专题:《yii框架入门及常用技巧总结》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总

希望本文所述对大家基于yii框架的php程序设计有所帮助。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网