Skip to main content

Introduction

Jinya Database is a lightweight and easy-to-use database struct mapper for PHP 8.3+, resembling some features of an ORM. It is designed to be simple, fast, and stays out of your way.

Features

  • Simple Mapping: Map database tables to PHP classes using PHP 8 attributes.
  • Multiple Databases: Support for MySQL, PostgreSQL, and SQLite through PDO.
  • Migration System: Integrated tool for managing database schema changes.
  • Query Builder: Integration with Aura.Sql-Query for powerful and flexible queries.
  • Auto Conversion: Automatically converts common types like DateTime between PHP and SQL.
  • Caching: Efficient caching mechanism for entity metadata to ensure high performance.

Installation

You can install Jinya Database via Composer:

composer require jinya/database

Quick Start

1. Configure the connection

Before you can use Jinya Database, you need to configure the connection. Typically, you do this in your application's bootstrap file.

use function Jinya\Database\configure_jinya_database;

configure_jinya_database(
cacheDirectory: __DIR__ . '/var/cache',
connectionString: 'mysql:host=localhost;dbname=my_database',
username: 'db_user',
password: 'db_password'
);

2. Define an Entity

Entities are simple PHP classes that extend Jinya\Database\Entity.

namespace App\Entities;

use Jinya\Database\Entity;
use Jinya\Database\Attributes\Column;
use Jinya\Database\Attributes\Id;
use Jinya\Database\Attributes\Table;

#[Table('artists')]
class Artist extends Entity
{
#[Id]
#[Column(autogenerated: true)]
public int $id;

#[Column]
public string $name;

#[Column]
public string $email;
}

3. Use the Entity

// Find an artist by ID
$artist = Artist::findById(1);

if ($artist) {
echo $artist->name;

// Update the artist
$artist->name = 'New Name';
$artist->update();
}

// Create a new artist
$newArtist = new Artist();
$newArtist->name = 'Jinya';
$newArtist->email = 'jinya@example.com';
$newArtist->create();

// List all artists
$artists = Artist::findAll();
foreach ($artists as $artist) {
echo $artist->name . PHP_EOL;
}