dev-master
9999999-dev https://github.com/phpf/DatabasePhpf Database package
MIT
The Requires
- php >=5.3
- ext-pdo *
- phpf/common dev-master
by The PHPf Developer
orm database pdo fluent
Phpf Database package
Database abstraction layer for PHP 5.3+ using PDO., (*1)
PDO
FluentPDO
(included)Phpf\Util
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 );
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)
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'); }
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...
Phpf Database package
MIT
orm database pdo fluent