luminar-organization / database
Database component of the Luminar PHP framework, handling connections, ORM, and query building.
Requires
- php: >=8.2
- ext-pdo: *
- luminar-organization/core: dev-main
Requires (Dev)
- phpunit/phpunit: >=11.3.1
This package is auto-updated.
Last update: 2025-04-05 17:35:29 UTC
README
The Luminar Database package is a core component of the Luminar PHP framework. It provides database connection management, ORM functionality, schema handling, and query building to create a simple, yet powerful interface for working with databases in PHP.
Features
- Database Connection Management: Manage multiple database connections with ease.
- ORM (Object-Relational Mapping): Interact with your database using PHP models.
- Query Builder: Build complex SQL queries using fluent, object-oriented interface.
- Schema Builder: Create, modify, and manage your database schema with ease.
- Migration Manager: Export and import your database to easily migrate between servers.
Installation
To use the Luminar Database package, install iti via Composer:
composer require luminar-organization/database
Usage
Database Connection
To create a database connection, use the Connection
class. For example, connecting to a MySQL server database:
use Luminar\Database\Connection\Connection; $connection = new Connection("mysql:host=localhost;dbname=example-database", "example_user", "example_password")
Query Builder
The query builder provides a fluent interface for building and executing SQL queries:
use Luminar\Database\ORM\QueryBuilder; $query = (new QueryBuilder()) ->table("users") ->where("id", '=', 1) ->limit(1) ->get(); echo $query; // Output: SELECT * FROM users WHERE id = 1 LIMIT 1;
ORM (Object-Relational Mapping)
Create your own Entity with Entity,Column annotations:
use Luminar\Database\ORM\Entity; use Luminar\Database\ORM\Column; use Luminar\Database\ORM\EntityManager; use Luminar\Database\Connection\Connection; #[Entity(name: "users")] // Table name class User { /** * @var int $id */ #[Column(name: "id")] private int $id; /** * @var string $username */ #[Column(name: "username")] private string $username; /** * @return string */ public function getUsername(): string { return $this->username; } /** * @param string $username */ public function setUsername(string $username):void { $this->username = $username; } /** * @return int */ public function getId(): int { return $this->id; } /** * @param int $id */ public function setId(int $id):void { $this->id = $id; } } $connection = new Connection("your dsn", "example_username", "example_password"); $entityManager = new EntityManager($connection); $schema = $entityManager->schema($entityManager); $connection->query($schema);
Schema Builder
The SchemaBuilder
helps you manage your database schema programmatically:
use Luminar\Database\Schema\SchemaBuilder; use Luminar\Database\Connection\Connection; $connection = new Connection("your dsn", "example_username", "example_password"); $schemaBuilder = new SchemaBuilder($connection); // Create a table $sql = $schemaBuilder->create('users', function ($table) { $table->addColumn('int', 'id'); $table->addColumn('varchar', 'name'); }); $connection->query($sql); // Drop a table $sql = $schemaBuilder->drop('users'); $connection->query($sql);
Migration Manager
The MigrationManager
helps you migrating your database:
use Luminar\Database\Connection\Connection; use Luminar\Database\Migration\MigrationManager; $connection = new Connection("your dsn", "example_username", "example_password"); $migrationDir = "YOUR_PATH"; $migrationManager = new MigrationManager($migrationDir, $connection); // Export Your Database $export = $migrationDir->export(); // Returns migration name `migrate-XXXXX` // Importing Database by migration name $migrationDir->import($export);
Testing
Unit tests are provided to ensure the functionality of the luminar-organization/databse
package. To run tests, use:
composer run test
Contribution
Checkout our CONTRIBUTION.md in our core package
License
The Luminar Database package is open-sourced software licensed under the MIT License