https://hyperf.wiki/3.0/#/zh-cn/db/quick-start?id=前言
#创建数据库模型
php bin/hyperf.php gen:model table_name
config\autoload\databases.php
https://laravel.com/docs/5.8/queries
https://hyperf.wiki/3.0/#/zh-cn/db/quick-start?id=执行原生-sql-语句
use Hyperf\DbConnection\Db;
$users = Db::select('SELECT * FROM `user` WHERE gender = ?',[1]); // 返回array
foreach($users as $user){
echo $user->name;
}
#Execute 执行类Insert、Update、Delete,属性为 MODIFIES SQL DATA 的存储过程等执行语句
$inserted = Db::insert('INSERT INTO user (id, name) VALUES (?, ?)', [1, 'Hyperf']); // 返回是否成功 bool
$affected = Db::update('UPDATE user set name = ? WHERE id = ?', ['John', 1]); // 返回受影响的行数 int
$affected = Db::delete('DELETE FROM user WHERE id = ?', [1]); // 返回受影响的行数 int
$result = Db::statement("CALL pro_test(?, '?')", [1, 'your words']); // 返回 bool CALL pro_test(?,?) 为存储过程,属性为 MODIFIES SQL DATA
use Hyperf\DbConnection\Db;
Db::transaction(function () {
Db::table('user')->update(['votes' => 1]);
Db::table('posts')->delete();
});
#出现一个异常,事务将会回滚。如果事务闭包 Closure 执行成功,事务将自动提交
Db::table('default')->update($data);
// test数据库链接表修改
Db::connection('test')->table('test')->update($data);