jetea / database
Jetea数据库组件。
2.0.0
2019-01-23 08:14 UTC
Requires
- php: >=7.1.3
Requires (Dev)
- phpunit/phpunit: ^5.4
This package is auto-updated.
Last update: 2024-09-25 23:00:09 UTC
README
Jetea数据库组件。
当前支持以下数据库供应商
- MySQL
- PostgreSQL
概述
安装
composer require jetea/database=~2.0 -vvv
使用
获取连接
- MySql
$dsn = sprintf('mysql:host=%s;port=%s;dbname=%s;charset=%s', '127.0.0.1', 3306, 'es_demo', 'utf8mb4');
$conn = new MySqlConnection($dsn, 'root', 123123);
- Postgres
$dsn = sprintf('pgsql:host=%s;port=%s;dbname=%s', '127.0.0.1', 3306, 'es_demo');
$conn = new PostgresConnection($dsn, 'root', 123123);
插入并获取插入ID
$insertId = $conn->table('profile')->insertGetId([
'name' => 'test-name',
'gender' => 1,
'birthday' => '1988-12-01 01:00:01', //DATETIME
'memo' => 'this is a test memo',
'lat' => '30.54916000', //DECIMAL(10,8)
'lng' => '104.06761000' //DECIMAL(11,8)
]);
插入并获取影响的行数
$affectNum = $conn->table('profile')->insert([
[
'name' => 'test-name',
'gender' => 1,
'birthday' => '1988-12-01 01:00:01',
'memo' => 'this is a test memo',
'lat' => '30.54916000',
'lng' => '104.06761000'
],
[
'name' => 'test-name-1',
'gender' => 1,
'birthday' => '2010-12-01 01:00:01',
'memo' => 'this is another test memo',
'lat' => '30.54916000',
'lng' => '104.06761000'
],
]);
更新并获取影响的行数
affectNum = $conn->update('update profile set name = :name, memo = :memo where id = :id', [
':name' => 'test-name',
':memo' => 'this is another memo',
':id' => $id,
]);
选择
$records = $conn->select('select * from profile where id = :id', [
':id' => $id,
]);
删除并获取影响的行数
$affectNum = $conn->delete('delete from profile where id = :id', [
':id' => $id,
]);
事务
$conn->transaction(function ($conn) {
//do something...
});
获取查询日志
$queryLogs = $conn->getQueryLog();
在“空转”模式下执行给定的回调
$conn->pretend(function ($conn) {
//do something...
});