https://github.com/drago-ex/database
:floppy_disk: Simple and flexible database abstraction layer built on Dibi for PHP, with support for entities and repositories.
https://github.com/drago-ex/database
database dibi nette
Last synced: 5 months ago
JSON representation
:floppy_disk: Simple and flexible database abstraction layer built on Dibi for PHP, with support for entities and repositories.
- Host: GitHub
- URL: https://github.com/drago-ex/database
- Owner: drago-ex
- License: mit
- Created: 2015-10-16T06:58:42.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2026-01-16T08:27:02.000Z (5 months ago)
- Last Synced: 2026-01-16T23:33:37.170Z (5 months ago)
- Topics: database, dibi, nette
- Language: PHP
- Homepage:
- Size: 45 MB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license
Awesome Lists containing this project
README
## Drago Database
Simple recurring questions.
[](https://raw.githubusercontent.com/drago-ex/database/master/license)
[](https://badge.fury.io/ph/drago-ex%2Fdatabase)
[](https://github.com/drago-ex/database/actions/workflows/tests.yml)
[](https://github.com/drago-ex/database/actions/workflows/coding-style.yml)
[](https://www.codefactor.io/repository/github/drago-ex/database)
[](https://coveralls.io/github/drago-ex/database?branch=master)
## Requirements
- PHP >= 8.3
- Nette Framework
- dibi
- Composer
## Knowledge
- [Dibi - smart database layer for PHP](https://github.com/dg/dibi)
## Installation
```
composer require drago-ex/database
```
## Basic Model Example
```php
#[Table('table_name', 'primary_key')]
class Model
{
use Database;
}
```
## Common Queries
Reading records from a table:
```php
$this->model->read('*');
```
Find records by column name:
```php
$this->model->find('column, 'value');
```
Get a record by ID:
```php
$this->model->get(1);
```
Delete a record by column name:
```php
$this->model->delete('column, 'value');
```
Save records as an array (update if `id` is provided):
```php
$this->model->save(['column' => 'value']);
```
## Using Entities
```php
class SampleEntity extends Drago\Database\Entity
{
public const Table = 'name';
public const PrimaryKey = 'id';
public ?int $id = null;
public string $sample;
}
```
Use the entity in a model:
```php
#[From(SampleEntity::Table, SampleEntity::PrimarKey)]
class Model
{
use Database;
}
```
Fetch records as objects:
```php
$row = $this->model->find('id', 1)->record();
// Accessing properties
echo $row->id;
echo $row->sample;
```
## Save Entity Records
To save entity data (update record if `id` is present):
```php
$entity = new SampleEntity;
$entity->id = 1;
$entity->sample = 'sample';
$this->save($entity);
```
# Advanced Features
## Entity Class for Database Mapping
You can use a custom entity class with database mapping:
```php
/** @extends Database */
#[From(SampleEntity::Table, SampleEntity::PrimaryKey, class: SampleEntity::class)]
class Model
{
use Database;
}
// Fetch records directly as objects
$row = $this->model->find('id', 1)->record();
// Access the object's properties
echo $row->id;
echo $row->sample;
// Fetch all records
$allRecords = $this->model->read('*')->recordAll();
```
## Entity Generation
For automatic entity generation, consider using the Drago Generator tool: [https://github.com/drago-ex/generator](https://github.com/drago-ex/generator)