PHP code example of koalaphp / database

1. Go to this page and download the library: Download koalaphp/database 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/ */

    

koalaphp / database example snippets


$defaultDatabaseConfig = [
    'test' => [
        'master' => [
            'dbname' => 'test',
            'host' => '127.0.0.1',
            'port' => 3306,
            'user' => 'root',
            'pass' => 'yourpassword',
            'charset' => 'utf8mb4',
        ],
        'slaves' => [
            [
                'dbname' => 'test',
                'host' => '127.0.0.1',
                'port' => 3306,
                'user' => 'root',
                'pass' => 'yourpassword',
                'charset' => 'utf8mb4',
            ]
        ]
    ],
];
// 初始化配置
Koala\Database\Connection::initDatabaseConfig($defaultDatabaseConfig);
// 打开输出到屏幕的日志
Koala\Database\DBLogger::$isPrint = true;
// 设置当前的日志,可改为自己的日志处理类,推荐是继承自Monolog的logger类(需要实现了 "info" 方法)
Koala\Database\DBLogger::setLogger(new MyLogger());

/**
 * 插入一行数据到数据库中
 *
 * @param $insertData array 插入的数组
 * @param bool $isReturnId 是否返回插入的ID,默认是返回
 * @return int
 */
public function insertRow($insertData, $isReturnId = true);

$userDao = \Library\Dao\Test\UserDao::getInstance();
$insertData = [
    'name' => 'han',
    'phone' => '13688888888',
    'password' => password_hash("123456", PASSWORD_DEFAULT),
    'money' => 10.12,
    'remark' => NULL,
    'status' => true,
    'create_time' => time(),
    'update_time' => time(),
];
$res = $userDao->insertRow($insertData, true);

/**
 * 通过ID(主键)更新特定的一行记录
 *
 * @param int $id 主键ID
 * @param array $updateData 更新的数据
 * @return bool
 */
public function updateRow($id, $updateData);

/**
 * 通过ID(主键)列表来更新特定的多行记录
 *
 * @param array $idList ID(主键)列表
 * @param array $updateData  更新的数据
 * @param bool $isReturnEffectRows 是否返回影响的行数
 * @return bool
 */
public function updateMultiRows($idList, $updateData, $isReturnEffectRows = true);

$userDao = \Library\Dao\Test\UserDao::getInstance();
$updateData = [
    'phone' => '13688888877',
    'status' => 1,
    'update_time' => time(),
];
$res = $userDao->updateMultiRows([11, 12], $updateData);

/**
 * 通过ID(主键)删除特定的一行记录
 * @param int $id 主键ID
 * @return bool
 */
public function deleteRow($id = 0);

/**
 * 查询符合条件的某一个对象
 * $conditions 的详细用法参见findAllCore
 * @param array $conditions 查询where条件语句
 * @param string $sort 排序方式,默认"id desc"
 * @return \stdClass|null
 */
public function findOne($conditions = [], $sort = "id desc");

/**
 * 获取该查询条件下的总数
 * @param array $conditions 查询where条件语句
 * @return int
 */
public function findCount($conditions = []);

/**
 * 用来进行查询获取记录的方法
 * @param array $conditions 查询where条件语句
 * @param string $sort 可选 排序方式 默认为根据主键降序,当 $sort 为空字符串的时候,表示不进行 order by。
 * @param int $offset 可选 偏移量 默认:0
 * @param int $limit 可选 单次查询出的个数 默认:20,特殊情况当limit 等于 -1 时表示查找全部。
 * @param array $fieldList 可选 查询的字段列表 默认查出所有字段
 * @return array
 */
public function findAllRecordCore($conditions = [], $sort = "id desc", $offset = 0, $limit = 20, $fieldList = []);

/**
 * 可以实现循环获取所有行的功能(适用于在定时任务中扫描一整张表)
 *
 * 生成指定条件的迭代器
 * @param array $conditions 查询where条件语句
 * @param int $numPerTime 单次获取记录的个数
 * @param bool $isBatch 默认true, false: 一次返回1个结果,true:一次批量返回 {$numPerTime} 个结果
 * @return \Generator
 */
public function createGenerator($conditions = [], $numPerTime = 100, $isBatch = true);

$userDao = \Library\Dao\Test\UserDao::getInstance();
foreach ($userDao->createGenerator(["id" => [">" => 1]], 10, true) as $userObjList) {
    foreach ($userObjList as $userObj) {
        //... 
    }
}

    SQL: `status` = 1 AND `create_time` >= 1514713145 AND `create_time` <= 1514813145
    $conditions = [
        'status' => 1,
        'create_time' => [
            ">=" => 1514713145,
            "<=" => 1514813145
        ]
    ];

// 获取ShardingUserDao单例
$shardingUserDao = \Library\Dao\Test\ShardingUserDao::getShardingInstance("user01");
// 插入
$insertData = [
    'name' => 'han',
    'phone' => '13688888888',
    'password' => password_hash("123456", PASSWORD_DEFAULT),
    'money' => 10.12,
    'remark' => NULL,
    'status' => true,
    'create_time' => time(),
    'update_time' => time(),
];
$curId1 = $shardingUserDao->insertRow($insertData, true);

// 更新示例
$updateData = [
    'phone' => '13688888899',
    'status' => 1,
    'update_time' => time(),
];
$res = $shardingUserDao->updateRow($curId1, $updateData);

// 查询示例
$userObj = $shardingDao->findOne(["id" => $curId1]);