PHP code example of orz / dbh

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

    

orz / dbh example snippets


        // 从框架容器中取出dbh单例
        $dbi = service('dbh');
        // 从dbi实例中取出只读实例
        $ro = $dbi->$dbh('ro');
        // 从dbi实例中取出读写实例,默认参rw为读写实例
        $rw = $dbi->$dbh();
    

        // CRUD,获取单条记录    
        $row = $ro->get('_plog', '*', ['id'=>1,] );
        echo $ro->last(); // SELECT * FROM `_plog` WHERE `id` = 1 LIMIT 1

        // CRUD,获取多条记录
        $rows = $ro->select('_plog', '*', ['severity'=>'INFO',] );
        echo $ro->last(); // SELECT * FROM `_plog` WHERE `severity` = 'INFO'
        $rows = $ro->select('_plog', ['id','time','severity','message'], ['severity'=>'INFO',] );
        echo $ro->last(); // SELECT  `id`,`time`,`severity`,`message` FROM `_plog` WHERE `severity` = 'INFO'

        // CRUD,添加记录,若需用SQL原生函数可用raw函数处理
        $dat = ['time'=>$rw->raw('NOW()'), 'severity'=>'INFO', 'message'=>'info1', ];
        $pdostmt = $rw->insert('_plog', $dat);
        echo $rw->last(); // INSERT INTO `_plog` (`time`, `severity`, `message`) VALUES (NOW(), 'INFO', 'info1')
        // CRUD,获最记录的自增ID
        $id = $rw->id();

        // CRUD,更新记录,若需用SQL原生函数可用raw函数处理
        $dat = ['time'=>$rw->raw('NOW()'), 'severity'=>'INFO', 'message'=>'info111', ];
        $pdostmt = $rw->update('_plog', $dat, ['id'=>1]);
        echo $rw->last(); // UPDATE `_plog` SET `time` = NOW(), `message` = 'info111' WHERE `id` = '1'

        // CRUD,删除记录
        $pdostmt = $rw->delete('_plog', ['id'=>1]);
        echo $rw->last(); // DELETE FROM `_plog` WHERE `id` = '1'

        // 怎样知道是否错,判断实例属性error是否有值, 实例属性errorInfo得到出错细节
        if($rw->error){
            print_r($rw->errorInfo);
        }

        // 多表联查,数据排序,数据分页,批量操作,事务处理,等进阶用法更多细节请查阅Medoo官方文档
        // 脱离框架使用dbh请参考 demo/Dbt2.php
    

        // 从框架容器中取出dbh单例
        $dbi = service('dbh');
        // 从dbi实例中取出Plog实例,默认参db为数据库表记录日志
        $log = $dbi->$wlog('db');
    

        // 6个日志级别,info, notice, debug, warning, error, fatal,均支持快捷操作,必填参数1为文本日志信息,可选参数2为附带数据
        $plog->info('HelloWorld');
        $plog->info('HelloWorld', ['data'=>[123,]]);
        $plog->debug("I'mHere",['data'=>['123','456',"I'm"]]);        
    

    public $psr4 = [
        APP_NAMESPACE => APPPATH, // For custom app namespace
        'Config'      => APPPATH . 'Config',
        'Dbh\\CodeIgniter' => ROOTPATH.'vendor\\orz\\dbh\\src\\CodeIgniter\\',
    ];
    

    'providers' => [
        //My Dbh Service Providers...
        Dbh\Laravel\Providers\DbhServiceProvider::class,
    ],
    'aliases' => [
        // My Dbh Facade...
        'Dbf' => Dbh\Facades\DbhFacade::class,
    ]       
    

        
        use app\AppService;
        return [
            AppService::class,
            // myService
            \Dbh\thinkphp\service\DbhService::class,
        ]; 
    

    
    return [
        'dbh'=>Dbh\thinkphp\provider\DbhProvider::class
    ];
    

    
    namespace app\index\controller;

    use think\Request;
    use Dbh\thinkphp\provider\DbhProvider;

    class Index extends Controller {

        protected $dbi = null;

        function _initialize(){                
            $this->dbi = DbhProvider::invoke($this->request);
            parent::_initialize();
        }
        /**
         * 测试1,属性注入后用助手函数request()调用
         * http://test12.localhost.localdomain/index/index/test1
         */
        function test1(){
            $result = 'HelloThinkphp50';

            $rw = request()->dbh();
            $pdostmt = $rw->query('SELECT version() as version');
            $version = $pdostmt->fetchAll();
            var_dump($version); // [['version':'5.7.1']]

            $log = $this->dbi->wlog('db');
            $log->info($result);

            return $result;
        }
    }
    

    use Dbh\Laravel\Facades\DbhFacade as Dbf;
    Dbf::wlog('db')->info('helloworld,1234'); 
    $podstmt = Dbf::dbh()->query('SELECT version() AS version');
    print_r($podstmt->fetchAll());
    

    use Dbh\thinkphp\facade\DbhFacade as Dbf;
    Dbf::wlog('db')->info('helloworld,1234');     
    $podstmt = Dbf::dbh()->query('SELECT version() AS version');
    print_r($podstmt->fetchAll());
    
sql
    SELECT * FROM _plog;