Hubert

Database

Hubert uses zend-db as database adapter.

Installation

At first you have to extend the configuration of composer:

{
    "require": {
        "falkm/hubert-db": "1.*"
    }
}

Configuration

Afterwards you have to extend the configuration of Hubert or create a new config/database.global.php file. In this configuration you have to define the database connection.

<?php
return array(
    "factories" => array(
         "dbAdapter" => array(hubert\extension\db\factory::class, 'get')
    ),
    "config" => array(
        "db" => array(
            'driver'   => 'Pdo_Mysql',
            'database' => 'db_test',
            'username' => 'user',
            'password' => 'pass',
        ),
    ),
);

Usage

$result = hubert()->dbAdapter->query('SELECT * FROM `db_test` WHERE `id` = :id', ['id' => 1]);
print_r($result->current());

This example shows how to request some data from a database and return the frst row. More descriptions on database usage can be found at docs.zendframework.com/zend-db/.

Models

The Hubert extension also contains a structure for models. For instance you can create a model/user.php file:

<?php

namespace model;

class user extends \hubert\extension\db\model {

    protected static $table = "user";

    public static function fields(){
        return array(
            "id" => array('type' => 'integer', 'primary' => true, 'autoincrement' => true),
            "name" => array('type' => 'string', "default" => ""),
            "password" => array('type' => 'string', "default" => "")
        );
    }

    public function update($rows = array()){
        $update = array();
        foreach ($rows as $row){
            $update[$row] = $this->$row;
        }
        return static::tableGateway()->update($update, ["id" => $this->id]);
    }

    public function getRoleIds(){
        $query = "SELECT role_id FROM user_role_mapping WHERE user_id = :user_id";
        $result = hubert()->dbAdapter->query($query, array("user_id" => $this->id));
        $role_ids = array();
        foreach ($result as $res){
            $role_ids[] = $res["role_id"];
        }
        return $role_ids;
    }

}

All models contain the static functions selectOne($where), count($where = array()) and selectAll($where = array(), $limit = 0, $offset = 0) by default:

print_r(json_encode(\model\user::selectOne(["id" => 1])));
print_r(json_encode(\model\user::selectAll()));

The next example shows how to change values of a user in a databse or how to get an array with its role id's:

$user = \model\user::selectOne(["id" => 1]);
$user->name = "hubert";
$user->update(["name]);
print_r($user->getRoleIds());