kisphp / dbal
Kisphp Database Access Layer
0.2.0
2021-10-20 10:53 UTC
Requires
- php: >=7.4
- doctrine/dbal: ~3.0
Requires (Dev)
- ext-json: *
- phpunit/phpunit: ~9.5
- symfony/var-dumper: ~3.4|~5.0
This package is auto-updated.
Last update: 2024-11-05 09:27:23 UTC
README
Installation
Run in terminal
composer require kisphp/dbal
Connect to database
<?php
require_once 'path/to/vendor/autoload.php';
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\DriverManager;
use Kisphp\Db\KisphpDbal;
$config = new Configuration();
$connectionParams = [
'driver' => 'pdo_mysql', // or pdo_pgsql
'host' => 'localhost',
'dbname' => 'test',
'user' => 'root',
'password' => '',
'charset' => 'utf8',
'driverOptions' => [
1002 => 'SET NAMES utf8',
]
];
$connection = DriverManager::getConnection($connectionParams, $config);
$db = new KisphpDbal($connection);
Database Insert
$db->insert('table_name', 'data array');
$db->insert('test_table', [
'column_1' => 'value_1',
'column_2' => 'value_2',
]);
// will return last_insert_id
Database update
$db->update('table_name', 'data array', 'condition value', 'column name (default=id)');
$db->update('test_table', [
'column_1' => 'value_1',
'column_2' => 'value_2',
], [
'id' => 1
]);
// will return affected_rows
Get single value
$value = $db->getValue("SELECT column_1 FROM test_table");
// or
$value = $db->getValue("SELECT column_1 FROM test_table WHERE id = :id", [ 'id' => 1 ]);
Get pairs
$pairs = $db->getPairs("SELECT id, column_1 FROM test_table");
// or
$pairs = $db->getPairs("SELECT id, column_1 FROM test_table WHERE column_2 = :condition", [ 'condition' => 'demo' ]);
/*
will result
$pairs = [
'1' => 'c1.1',
'2' => 'c2.1',
'3' => 'c3.1',
];
*/
Get Custom query
$query = $db->query("SELECT * FROM test_table");
// or
$query = $db->query("SELECT * FROM test_table WHERE column = :condition", [ 'condition' => 'demo' ]);
while ($item = $query->fetch(\PDO::FETCH_ASSOC)) {
var_dump($item);
}