无用的/pdo-wrapper

带配置的PDO包装器

dev-master 2015-01-05 09:52 UTC

This package is not auto-updated.

Last update: 2024-09-28 16:51:47 UTC


README

PHP PDO包装器带配置

配置工厂

Config\Factory 允许你动态实例化一个实现 ConfigInterface 的类,如果你的当前配置没有使用标准的 PDO DSN。

<?php
use PDO;
use Useless\Pdo\Config\Factory;
use Useless\Pdo\Config\AbstractConfig;

$config = array(
	AbstractConfig::DRIVER => Factory::CONFIG_MYSQL,
	AbstractConfig::HOST => 'localhost',
	AbstractConfig::DBNAME => 'test',
	AbstractConfig::CHARSET => 'utf8',
	AbstractConfig::USERNAME => 'username',
	AbstractConfig::PASSWORD => 'password',
);

$factory = new Factory();
$config = $factory->getConfig( $config );
$pdo = new PDO( $config->getDsn(), $config->getUsername(), $config->getPassword(), $config->getOptions() );
foreach ( $config->getAttributes() as $attribute => $value ) {
	$pdo->setAttribute( $attribute, $value );
}

延迟连接

实例化可以与原生的 PDO 类相同:数据源名称、用户名、密码和驱动选项。一个额外的参数允许在连接建立后传递要设置的属性。

<?php
use Useless\Pdo\Wrapper;

$pdo = new Wrapper(
	'mysql:localhost;dbname=test;charset=utf8',
	'username',
	'password',
	array(), # driver options as key/value pairs
	array()  # attributes to be set after connection as key/value pairs
);

或者你可以使用扩展了 Useless\Pdo\ConfigInterface 的类进行实例化。

<?php
use Useless\Pdo\Wrapper;

$config = $factory->getConfig( $config );

$pdo = new Wrapper( $config );