承接 tnapf/pdo 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

tnapf/pdo

最新稳定版本:v1.0.1

Composer 安装命令:

composer require tnapf/pdo

包简介

A PDO driver with additional features

README 文档

README

Making PDO easier with the power of magic

Installation

composer require tnapf/pdo

Creating a connection

Doing so manually

$driver = (new Driver)
	->withUsername("root")
	->withPassword("password")
	->withDatabase("database")
	->withHost("127.0.0.1")
	->withPort(3306)
	->withPrefix(Driver::PREFIX_MYSQL)
	->connect()
;

You can use Driver::setDsnProp or Driver::with{insert dsn prop name here} to set additional dsn values

Using the database predefined createDriver method

I currently only have mysql, postgres, and SQLite specific methods

$driver = Driver::createMySqlDriver("root", "password", "database")->connect();
$driver = Driver::createPostgresSqlDriver("root", "password", "database")->connect();
$driver = Driver::createSQLiteDriver("/path/to/db.sqlite")->connect();

// you can set the host and port in the last two parameters but they default to localhost and the default port of the service

Executing a query

$rows = $driver->query("SELECT * FROM table")->fetchAll(PDO::FETCH_ASSOC);

The driver will store the PDOStatement internally and detect if the method you're invoking exists in PDOStatement or PDO and invoke it on one of the instances accordingly. Thankfully there are no method names that are the same between the two classes

Preparing a statement

$driver->prepare("SELECT * FROM table WHERE column = :column");
$driver->bindValue("column", "value");
$driver->execute();
$rows = $driver->fetchAll(PDO::FETCH_ASSOC);

Singleton Constructor Argument

If you construct a Driver with the singleton argument as true then that new instance will be stored as a static property in the class that can be called from anywhere with the get method. You can additionally call PDO/PDOStatement methods statically from Driver and it will work similar to $driver->methodName

(new Driver(true))
	->withUsername("root")
	->withPassword("password")
	->withDatabase("database")
	->withHost("127.0.0.1")
	->connect()
;

function getRowWhereIdIs(int $id, int $fetch_type = PDO::FETCH_ASSOC): mixed
{
	Driver::prepare("SELECT * FROM table WHERE id = :id");
	Driver::bindValue("id", $id);
	Driver::execute();
	return Driver::fetch($fetch_type);
}

$row = getRowWhereIdIs(20);

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-02-12