PHP code example of zwei / brief-db

1. Go to this page and download the library: Download zwei/brief-db library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

zwei / brief-db example snippets


{
    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/qq1060656096/brief-db.git"
        }
    ],
    "


//数据库配置
return [
    // 默认数据库配置
   'default' => [
       'driver'    => 'mysql',// msyql驱动
       'host'      => 'localhost',// 主机
       'port'      => 3306,// 端口
       'user'      => 'root',// 账户
       'password'  => 'root',// 密码
       'dbname'    => 'demo',// 数据库名
       'table_prefix'  => 'tbl_',// 表前缀
   ],
];


// 引入要用的类
use Zwei\BriefDB\Database\Db;

// 删除
Db::getDelete()
->from('test')->condition('name', 'test_01')->delete();

// 删除2
Db::getSelect()
->from('test')
//like查询
->condition('name', 'test_01%', 'like')
// in条件
->condition('name', ['xiao1','xiao2'])
//复杂条件
->conditionComplex("(age >= ? and age <= ?) or created like ?", [60, 70, '2017-05-18 17:28%'])
->delete();

// 插入
$insertData = [
    'name' => 'insert01',
    'age' => 1,
    'uid' => 1,
];
Db::getInsert()
    ->from('test')->insert($insertData);

// 批量插入
$insertData = [
    [
        'name' => 'insertAll01',
        'age' => 1,
        'uid' => 2,
    ],
    [
        'name' => 'insertAll02',
        'age' => 3,
        'uid' => 4,
    ]
];
$result = Db::getInsert()
    ->from('test')->insertAll($insertData);


// 更改
$updateData = [
    'name' => 'update01',
    'age' => 33,
    'uid' => [
        'raw' => "? + ? - ?",
        1,
        2,
        3
    ],
];
// set部分: `name` = '20170531.0848',`age` = '173710',`uid` = '1' + '2' - '3'
Db::getUpdate()
    ->from('test')->condition('name', 'update01')->update($updateData);


// 批量更改
$bathUpdate = new BatchUpdate();
$condition  = new Condition('AND');
$condition->condition('name', 'updateBatch01', 'like');
$data       = [
    'age' => 1815080100,
    'uid' => 18150801200,
    'created' => '2017-05-18 15:08:01',
];
$bathUpdate->addData($condition, $data);
$data = [
    'age' => 1815080200,
    'uid' => 18150802200,
    'created' => '2017-05-18 15:08:02',
];
$condition  = new Condition('AND');
$condition->condition('name', 'updateBatch02', 'like');
// in条件
$condition->condition('name', ['xiao1','xiao2']);
$bathUpdate->addData($condition, $data);
Db::getUpdate()
    ->from('test')->updateAll($bathUpdate);
    
    
// 查询
$select = Db::getSelect()
    ->fields('name,id')
    ->fields(['uid', 'age'])
    ->from('test')
    ->condition('name', 'QueryFactorSelect00%', 'like')
//  ->groupBy('age')
    ->orderBy('id', 'DESC');
// 查询单行数据
$row = $select->findOne();
// 查询多行数据
$rows = $select->findAll();
// 查询总条数
$count = $select->findCount();

// 关联查询
$select = Db::getSelect();
$select->from('test t1');
$select->condition('t2.name', 'SelectTest::testFindAll-20170518-1256%', 'like');
// in查询
$select->condition('t2.name', ['xiao1','xiao2']);
$select->fields('*');
// 左联查询
$select->leftJoin('test t2', 'on t2.id = t1.id');
// 右联查询
$select->rightJoin('test t3', 'on t3.id = t2.id');
// 内联查询
$select->innerJoin('test t4', 'on t4.id = t3.id');
$select->findAll();


// 获取执行sql
// 启用sql日志
Db::enabledSqlLog();
// 查询: SELECT count(*) FROM test WHERE name LIKE '20170816.2359.se%'
$count = Db::getSelect()->from('test')
    ->condition('name', '20170816.2359.se%', 'like')
    ->findCount();
// 获取执行sql
$rawSql = Db::getLastRawSql();
// 打印sql日志
print_r($rawSql);

// doctrine2原生sql执行
$sql = "SELECT count(*) FROM test";
Db::getConnection()->fetchColumn($sql);
Db::getConnection()->fetchAll($sql);

use Zwei\Base\Config\Config;
Config::get('table_prefix', 'db.php');