liquidbox/silex-pdo 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

liquidbox/silex-pdo

Composer 安装命令:

composer require liquidbox/silex-pdo

包简介

A PDO service provider for the Silex micro-framework

README 文档

README

GitHub release license Build Status Code Coverage Scrutinizer Code Quality Packagist

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 PDO connection 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 PDO connection 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 我们能提供哪些服务?
定制开发 / 二次开发

基于 liquidbox/silex-pdo 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 6.17k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 2
  • 点击次数: 8
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-11-04