Asf_Db


Introduction

Using the Asf_Db class PHP supports pdo drivers, 当前封装三种数据库(MySQL、SQLite、PostgreSQL)CURD,这是一个轻量级的接口

Asf_Db 类内部使用PHP魔术方法(__callStatic)去调用Asf_Db_AbstractAdapter类封装的所有方法

采用单例模式连接数据库

支持PDO原生类

在开启命名空间情况下(asf.use_namespace=1)类名为 Asf\Db

Class synopsis

<?php
final class Asf_Db
{
    public static boolean function init(array $configs [, int $adapter_id = 0 [, bool $reset = false]])
    public static boolean function initMysql(array $configs [, bool $reset = false])
    public static mixed function __callStatic(string $function_name, array $args)
    public static array function getLinks(void)
    public static Asf_Db_QueryBuilder_Select function QBS(void)
    public static Asf_Db_QueryBuilder_Insert function QBI([bool $ignore = false])
    public static Asf_Db_QueryBuilder_Update function QBU(void)
    public static Asf_Db_QueryBuilder_Delete function QBD(void)
}

Examples

Example #1

StorageRule class on path: asf.root_path/library/Storagerule.php

<?php
use Asf\Db;

class StorageRule
{
    static public function getStorageRule($table, $db_type = 'slave')
    {/*{{{*/
        $configs = array(
            'dsn' => array('host' => '127.0.0.1', 'dbname' => 'test', 'port' => 6666),
            'username' => 'test',
            'password' => 'AbcdefRDvgedf',
        );

        $db['master'] = $configs;
        $db['slave']  = $configs;
		
        Db::init($db[$db_type]);
        Db::setTable($table);
    }/*}}}*/
}

class IndexDao
{/*{{{*/
    public function init($db_type)
    {
        StorageRule::getStorageRule('test1', $db_type);
    }

    public function test()
    {
        $this->init('master');

        $data = array('user' => 'lisi-2', 'pass' => '1234567');
        var_dump(Db::insert($data));
    }
}/*}}}*/

Example #2

<?php
use Asf\Db;
use Asf\Loader;
use Asf\Application;

$configs = array(
    'asf' => array(
        'root_path' => realpath(dirname(__FILE__)),
    )
);

class IndexService
{
    public function indexAction()
    {
        Loader::get('IndexLogic')->test();
    }
}

class IndexLogic
{
    public function test()
    {
        Loader::get('IndexDao')->test();
    }
}

class IndexDao
{/*{{{*/
    public function init($db_type)
    {
        $configs = array(
            'dsn' => array('host' => '127.0.0.1', 'dbname' => 'test', 'port' => 6666),
            'username' => 'test',
            'password' => 'AbcdefRDvgedf',
        );

        $db['master'] = $configs;
        $db['slave']  = $configs;

        Db::init($db[$db_type]);
        Db::setTable('test1');
    }

    public function test()
    {
        $this->init('master');

        $data = array('user' => 'lisi-2', 'pass' => '1234567');
        var_dump(Db::insertIgnore($data));
    }
}/*}}}*/

$handle = new Application($configs);
$handle->run();