关于数据库

起初爬山虎内核引擎并不计划内置集成数据库中间件,目的是为了保持内核的精简性, 不过考虑到数据库的高频使用率以及开发方便,爬山虎内置封装了Medoo风格的轻量级数据库, 使用方法和Medoo官网完全一样,当然你也可以单独引入自己喜欢的DB包,这也很容易做到。

操作数据库

操作数据库非常简单,只需要简单的两部操作:

0x01:配置数据库

打开应用的数据库配置文件database.php, 新增如下代码:

<?php
return array(
    'dbo' => array(
        'test' => array(
            'database_type' => 'mysql',
            'database_name' => 'test',
            'server'        => '127.0.0.1',
            'username'      => 'root',
            'password'      => 'root',
            'charset'       => 'utf8'
        ),
    ),
);
0x02:使用数据库

所有的worker实例都自动拥有如下两组接口以获取DBO对象,之后的CRUD操作和Medoo完全一模一样:

$downloader->onAfterDownloader = function($downloader){
    //dbo single instance and we can pass the DSN string `test`
    $downloader->getDbo('test')->select('user', '*');

    //dbo single instance and we can pass the configuration array
    $config = Configurator::get('globalConfig/database/dbo/test')
    $downloader->getDbo($config)->select('user', '*');

    //dbo new instance and we can pass the DSN string `test`
    $downloader->newDbo('test')->select('user', '*');

    //dbo new instance and we can pass the configuration array
    $config = Configurator::get('globalConfig/database/dbo/test')
    $downloader->newDbo($config)->select('user', '*');
};

results matching ""

    No results matching ""