2017 © Pedro Peláez
 

library database

Phpf Database package

image

phpf/database

Phpf Database package

  • Saturday, January 31, 2015
  • by wells5609
  • Repository
  • 1 Watchers
  • 1 Stars
  • 10 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Components-Database

Database abstraction layer for PHP 5.3+ using PDO., (*1)

Requirements

  • PDO
  • FluentPDO (included)
  • Phpf\Util

Basic Usage

First, initialize the database settings; the connection itself is lazy-loaded., (*2)

use Phpf\Database\Database;

Database::init(
    'db_name', 
    'db_host',
    'db_user', 
    'db_password',
    'db_table_prefix',
    'pdo_driver', // must be a valid PDO driver
);

Schemas

Table schemas represent a single database table. They allow models to validate data, such as columns and indexes., (*3)

Create and register your table schemas. Note that the Database is a singleton, accessible via instance()., (*4)

Database::instance()->registerSchema(
    new \Phpf\Database\Table\Schema(array(
        'table_basename' => 'user',
        'columns' => array(
            'id' => 'INT(10) NOT NULL AUTO_INCREMENT',
            'name' => 'VARCHAR(255) NOT NULL',
            'email' => 'VARCHAR(255) NOT NULL',
            'password' => 'VARCHAR(255) NOT NULL'
        ),
        'primary_key' => 'id',
        'unique_keys' => array(
            'name' => 'name'
        ),
        'keys' => array(
            'email' => 'email'
        ),
    )
);

Install the tables using your favorite method, or use \Phpf\Database\Sql\Writer., (*5)

Models

Now we need to create a model (see Orm\Model). Models operate on a single table through its Database\Table object (created when a schema is registered). Models wrap around their Table objects to call corresponding Database methods., (*6)

Models must define the method getTableBasename(), which must return the table's base name (i.e. without a prefix). In this example, the user model would return user., (*7)

$cntrl = new UserModel();
$cntrl->getTableBasename(); // returns 'user'
$rows_affected = $cntrl->insert( array(
    'name' => 'This Guy',
    'email' => 'thisguy@gmail.com',
    'password' => 'gobbldygook'
) );

if ( 0 !== $rows_affected ){
        $new_user_id = $cntrl->select(array('name' => 'This Guy'), 'id');
}

Fluent

The Database object wraps FluentPDO, an excellent fluent interface for PDO. To use it, call Database::instance()->fluent()., (*8)

$db = Database::instance();
$fpdo = $db->fluent();

$fpdo->from('user')
    ->where(array('email', 'someguy@gmail.com'))
    // etc...

The Versions

31/01 2015

dev-master

9999999-dev https://github.com/phpf/Database

Phpf Database package

  Sources   Download

MIT

The Requires

 

by The PHPf Developer

orm database pdo fluent