Getting Started
To start using Sparkle, you need to initialize a database connection using the Manager class.
1. Autoload the package
Include Composer's autoloader in your project:
php
require 'vendor/autoload.php';2. Initialize the Manager
php
use Sirmerdas\Sparkle\Database\Manager;
$connection = new Manager(string|null $logPath = null, bool $failSilently = false)| Parameter | Type | Description |
|---|---|---|
$logPath | string|null | The file path where logs should be stored. Use null to disable logging. |
$failSilently | bool | If true, suppresses exceptions and prevents crashes due to sql errors. |
WARNING
When failSilently is true, If an error occurs, you may receive no result. This will also affect transactions, as no exception will be thrown. Use it with caution!
Example:
php
$connection = new Manager('app.log', true); // Enables logging and suppresses errors3. Add Database Connections
Method: addConnection
php
use Sirmerdas\Sparkle\Enums\Database;
$connection->addConnection(
Database $database,
string $host,
string $dbName,
string $username,
string $password,
string $charset = 'utf8',
array $pdoConnectionOptions = [],
string $connectionName = 'default'
): void| Parameter | Type | Description |
|---|---|---|
$database | \Sirmerdas\Sparkle\Enums\Database | Database type. Use AvailableDB::MYSQL, AvailableDB::PGSQL, etc. |
$host | string | Database server host (e.g., 'localhost' or IP address). |
$dbName | string | Name of the database to connect to. |
$username | string | Database user. |
$password | string | Database password. |
$charset | string | Character set (default: 'utf8'). |
$pdoConnectionOptions | array | Additional PDO options (default: []). |
$connectionName | string | The name of this connection (default: 'default'). |
Example:
php
use Sirmerdas\Sparkle\Enums\Database as AvailableDB;
$connection->addConnection(
AvailableDB::MYSQL, // Database type
'localhost', // Host
'sparkle', // Database name
'root', // Username
'password' // Password
);
// To add multiple database connections:
$connection->addConnection(
AvailableDB::MYSQL,
'localhost',
'exam',
'root',
'password',
connectionName: 'exam' // Custom connection name
);4. Boot the Manager
Method: boot
php
Manager::boot(string $connectionName = 'default'): void| Parameter | Type | Description |
|---|---|---|
$connectionName | string | The name of the connection to set as the default (default: 'default'). |
Example:
php
Manager::boot(); // Boots the default connection
Manager::boot('exam'); // Boots a specific connectionComplete Example:
php
use Sirmerdas\Sparkle\Database\Manager;
use Sirmerdas\Sparkle\Enums\Database as AvailableDB;
$connection = new Manager('app.log',true);
$connection->addConnection(AvailableDB::MYSQL, 'localhost', 'sparkle', 'root', 'password');
$connection->addConnection(AvailableDB::MYSQL, 'localhost', 'exam', 'root', 'password', connectionName: 'exam');
Manager::boot();Now you're ready to start using Sparkle to interact with your database! 🚀