Skip to content

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)
ParameterTypeDescription
$logPathstring|nullThe file path where logs should be stored. Use null to disable logging.
$failSilentlyboolIf 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 errors

3. 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
ParameterTypeDescription
$database\Sirmerdas\Sparkle\Enums\DatabaseDatabase type. Use AvailableDB::MYSQL, AvailableDB::PGSQL, etc.
$hoststringDatabase server host (e.g., 'localhost' or IP address).
$dbNamestringName of the database to connect to.
$usernamestringDatabase user.
$passwordstringDatabase password.
$charsetstringCharacter set (default: 'utf8').
$pdoConnectionOptionsarrayAdditional PDO options (default: []).
$connectionNamestringThe 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
ParameterTypeDescription
$connectionNamestringThe 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 connection

Complete 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! 🚀

Released under the MIT License.