Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

740 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Drago Database

Simple database helpers built on top of Dibi.

License: MIT PHP version Tests Coding Style

Requirements

  • PHP >= 8.3
  • Nette Framework
  • dibi
  • Composer

Installation

composer require drago-ex/database

Knowledge

Basic Model Example

#[Table('table_name', 'primary_key')]
class Model
{
    use Database;
}

Common Queries

Reading records from a table:

$this->model->read('*');

Find records by column name:

$this->model->find('column', 'value');

Get a record by ID:

$this->model->get(1);

Delete a record by column name:

$this->model->delete('column', 'value');

Save records as an array (update if id is provided):

$this->model->save(['column' => 'value']);

Using Entities

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:

#[Table(SampleEntity::Table, SampleEntity::PrimaryKey, entity: SampleEntity::class)]
class Model
{
	/** @phpstan-use Database<SampleEntity> */
    use Database;
}

Fetch records as objects:

$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):

$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:

#[Table(SampleEntity::Table, SampleEntity::PrimaryKey, entity: SampleEntity::class)]
class Model
{
	/** @phpstan-use Database<SampleEntity> */
    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

About

💾 Simple and flexible database abstraction layer built on Dibi for PHP, with support for entities and repositories.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages