Skip to content
This repository has been archived by the owner on Dec 25, 2020. It is now read-only.
Kerem Güneş edited this page Jan 5, 2017 · 34 revisions

Oppa Workflow

Oppa starts to work first with given configuration array and creates a fresh Database object. Goes to Linker to get created a Link object if not already connected to the same host even with master/slave directives (otherwise returns existing Link object). Assigns an Agent object (actually that guy does all stuff) to work with target database.

In Agent object, you can find a few nice tools like Batch, Result, Logger and Profiler that make the things easy for you. You will be also able to use many useful methods of it.

-> Configuration
-> Create a Database Object
   -> Create a Linker Object 
      -> Create a Link Object 
         -> Assign an Agent Object
            -> Batch Object (auto-created)
               -> "handle transactions"
            -> Result Object (auto-created)
               -> "handle query results"
            -> Logger (optional)
               -> "handle link/query/event/error logging"
            -> Profiler (optional)
               -> "handle link and mostly query profiling"

If it sounds complicated and don't wanna use it as is, take a look at these examples.

Simple OOP Implementation

use \Oppa\Database;

// define a proxy class
class Db
{
    private static $instance;
    private static $cfg = [
        'agent'    => 'mysqli',
        'database' => [
            'host'     => 'localhost', 'name'     => 'test',
            'username' => 'test',      'password' => '********',
            'charset'  => 'utf8',      'timezone' => '+00:00',
        ]
    ];
    private $db;

    // forbid idle actions
    private function __clone() {}
    private function __construct() {}

    // mighty singleton stuff
    public static function init() {
        if (self::$instance == null) {
            self::$instance = new self();
            self::$instance->db = new Database(self::$cfg);
            self::$instance->db->connect();
        }
        return self::$instance;
    }

    // proxy (shortcut) method
    public function query($sql, array $params = null) {
        return $this->db->getLink()->getAgent()->query($sql, $params);
    }
}

// get database instance
$db = Db::init();

// make a regular query
$users = $db->query("select * from users limit 3");
foreach ($users as $user) {
    print $user->name;
}

Simple Procedural Implementation

use \Oppa\Database;

static $cfg = [
    'agent'    => 'mysqli',
    'database' => [
        'host'     => 'localhost', 'name'     => 'test',
        'username' => 'test',      'password' => '********',
        'charset'  => 'utf8',      'timezone' => '+00:00',
    ]
];

// singleton trick (?)
function db() {
    if (!isset($GLOBALS['db'])) {
        global $cfg;
        $db = new Database($cfg);
        $db->connect();
        // store db object
        $GLOBALS['db'] = $db;
    }
    return $GLOBALS['db'];
}

// proxy (shortcut) function
function db_query($sql, array $params = null) {
    return db()->getLink()->getAgent()->query($sql, $params);
}

// make a regular query
$users = db_query("select * from users limit 3");
foreach ($users as $user) {
    print $user->name;
}