codememory / database-connection
数据库连接
v1.0
2021-10-10 23:23 UTC
Requires
- ext-pdo: *
Requires (Dev)
- symfony/var-dumper: ^5.3
This package is auto-updated.
Last update: 2024-09-11 05:35:24 UTC
README
安装
composer require codememory/database-connection
支持的驱动程序
- MySQL
- PostgreSQL
- SQLite
Connection 类的方法
getConnector(): ConnectorInterface
获取连接器- string $connectorName
getConnectors(): ConnectorInterface[]
获取所有连接器的数组reconnect(): ConnectorInterface|ConnectionInterface
重新定义连接器数据- string $connectorName
- callable $callback
- string|null $newConnectorName
connectorExist(): bool
检查连接器是否存在- string $connectorName
Connector 类的方法
isConnection(): bool
检查是否已连接到数据库getConnection(): PDO
如果连接成功,获取 PDO 对象getConnectorData(): ConnectorConfigurationInterface
获取已连接用户的配置数据getConnectorName(): string
获取当前连接器的名称
创建连接器的示例
<?php use Codememory\Components\Database\Connection\Connection; use Codememory\Components\Database\Connection\Interfaces\ConnectorConfigurationInterface; use Codememory\Components\Database\Connection\Drivers\MySQLDriver; require_once 'vendor/autoload.php'; $connection = new Connection(); // Добавление коннектора в список всех коннекторов $connection->addConnector('connector-name', function (ConnectorConfigurationInterface $configuration) { $configuration ->host('localhost') ->dbname('dbname') ->username('username') ->password('user password') ->driver(new MySQLDriver()) });
重定义连接器数据的示例
// Возьмем ранее добавленный коннектор "connector-name" $connection->reconnect('connector-name', function (ConnectorConfigurationInterface $configuration) { $configuration->dbname('new-dbname'); }); $connection ->getConnector('connector-name') ->getConnectorData() ->getDbname(); // new-dbname