liquidbox/silex-pdo
Composer 安装命令:
composer require liquidbox/silex-pdo
包简介
A PDO service provider for the Silex micro-framework
README 文档
README
You are reading the documentation for Silex 2.x. Switch to the documentation for Silex 1.x.
PHP Data Objects
The PdoServiceProvider provides integration with the PHP Data Objects (PDO) extension.
Parameters
- pdo.dsn (optional): The Data Source Name, or DSN, contains the information required to connect to the database.
- pdo.driver (optional): The PDO driver implementation to use.
- pdo.connection (optional): A collection of driver-specific connection parameters for specifying the connection string.
- pdo.username (optional): The user name for the DSN string.
- pdo.password (optional): The password for the DSN string.
- pdo.options (optional): A collection of driver-specific connection options.
- pdo.attributes (optional): A collection of attributes to set.
The parameters pdo.driver and pdo.connection are ignored if pdo.dsn is set.
Services
- pdo: The
PDOconnection instance. The main way of interacting with PDO. - pdo.connections: The collection of PDO connection instances. See section on using multiple databases for details.
- pdo.connect: Factory for
PDOconnection instances.
Registering
Example #1 Connecting to MySQL
$app->register(new \LiquidBox\Silex\Provider\PdoServiceProvider(), array( 'pdo.dsn' => 'mysql:host=localhost;dbname=test', 'pdo.username' => $user, 'pdo.password' => $passwd, )); // or $app->register(new \LiquidBox\Silex\Provider\PdoServiceProvider(), array( 'pdo.connection' => array( 'host' => 'localhost', 'dbname' => 'test' ), 'pdo.username' => $user, 'pdo.password' => $passwd, ));
The two registered connections are equivalent.
Example #2 Connecting to SQLite
$app->register(new \LiquidBox\Silex\Provider\PdoServiceProvider(), array( 'pdo.dsn' => 'sqlite::memory:', )); // or $app->register(new \LiquidBox\Silex\Provider\PdoServiceProvider(), array( 'pdo.driver' => 'sqlite', 'pdo.connection' => ':memory:', ));
Example #3 Using Doctrine service names
$app->register(new \LiquidBox\Silex\Provider\PdoServiceProvider('db', 'dbs'), array( 'pdo.driver' => 'pdo_pgsql', 'pdo.connection' => array( 'host' => 'localhost', 'dbname' => 'test', ), 'pdo.username' => $user, 'pdo.password' => $passwd, ));
The services pdo and pdo.connections will be renamed db and dbs respectively.
Add PDO as a dependency:
composer require liquidbox/silex-pdo:^2.0
Usage
Example #1 Demonstrate query
$sql = 'SELECT name, color, calories FROM fruit ORDER BY name'; foreach ($app['pdo']->query($sql) as $row) { echo implode("\t", $row) . PHP_EOL; }
Example #2 Prepare an SQL statement with named parameters
/* Execute a prepared statement by passing an array of values */ $sql = <<<heredoc SELECT name, color, calories FROM fruit WHERE calories < :calories AND color = :color heredoc; $pdoStatement = $app['pdo']->prepare($sql, array( \PDO::ATTR_CURSOR => \PDO::CURSOR_FWDONLY, )); $pdoStatement->execute(array(':calories' => 150, ':color' => 'red')); $red = $pdoStatement->fetchAll(); $pdoStatement->execute(array(':calories' => 175, ':color' => 'yellow')); $yellow = $pdoStatement->fetchAll();
Using multiple databases
The PDO provider can allow access to multiple databases. In order to configure the data sources, use pdo.dsn as an array of configurations where keys are connection names and values are parameters:
$app->register(new LiquidBox\Silex\Provider\PdoServiceProvider(), array( 'pdo.dsn' => array( 'mysql_read' => array( 'connection' => array( 'host' => 'mysql_read.someplace.tld', 'dbname' => 'my_database', 'charset' => 'utf8mb4', ), 'username' => 'my_username', 'password' => 'my_password', ), 'mysql_write' => array( 'connection' => array( 'host' => 'mysql_write.someplace.tld', 'dbname' => 'my_database', 'charset' => 'utf8mb4', ), 'username' => 'my_username', 'password' => 'my_password', ), ), ));
The first registered connection is the default and can simply be accessed as you would if there was only one connection. Given the above configuration, these two lines are equivalent:
$app['pdo']->query('SELECT * FROM table')->fetchAll(); $app['pdo.connections']['mysql_read']->query('SELECT * FROM table')->fetchAll();
You can use different drivers for each connection.
$app->register( new LiquidBox\Silex\Provider\PdoServiceProvider(null, 'pdo.dbs'), array( 'pdo.dsn' => array( 'member_db' => array( 'driver' => 'pdo_pgsql', 'connection' => array( 'host' => 'member_data.someplace.tld', 'dbname' => 'membership', ), 'username' => $pgsql_user, 'password' => $pgsql_passwd, ), 'content_db' => array( 'connection' => array( 'host' => 'content_data.someplace.tld', 'dbname' => 'media_info', 'charset' => 'utf8', ), 'username' => $mysql_user, 'password' => $mysql_passwd, ), 'session_storage' => array('dsn' => 'sqlite::memory:'), ), ) );
This registers $app['pdo.dbs']['member_db'], $app['pdo.dbs']['content_db'], and $app['pdo.dbs']['session_storage'] using PostgreSQL, MySQL, and SQLite drivers respectively.
Traits
LiquidBox\Silex\Application\PdoTrait adds the following shortcut:
- prepare: Prepares a statement for execution and returns a statement object.
/* Execute a prepared statement by passing an array of values */ $pdoStatement = $app->prepare('SELECT name, colour, calories FROM fruit WHERE calories < ? AND colour = ?'); $pdoStatement->execute(array(150, 'red')); $red = $pdoStatement->fetchAll(); $pdoStatement->execute(array(175, 'yellow')); $yellow = $pdoStatement->fetchAll();
For more information, check out the official PDO documentation.
liquidbox/silex-pdo 适用场景与选型建议
liquidbox/silex-pdo 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 6.17k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2017 年 11 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「silex」 「database」 「php」 「mysql」 「sqlite」 「postgresql」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 liquidbox/silex-pdo 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 liquidbox/silex-pdo 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 liquidbox/silex-pdo 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Dibi is Database Abstraction Library for PHP
Store your language lines in the database, yaml or other sources
A package for automatically encrypting and decrypting Eloquent attributes in Laravel 5.5+, based on configuration settings.
Object-oriented CRUD for Doctrine DBAL
A PSR-7 compatible library for making CRUD API endpoints
Command bus implementation: Commands and domain events
统计信息
- 总下载量: 6.17k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 8
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-11-04