https://github.com/drago-ex/database
:floppy_disk: Simple recurring questions. (@nette)
https://github.com/drago-ex/database
database dibi nette
Last synced: 4 months ago
JSON representation
:floppy_disk: Simple recurring questions. (@nette)
- Host: GitHub
- URL: https://github.com/drago-ex/database
- Owner: drago-ex
- License: mit
- Created: 2015-10-16T06:58:42.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2024-04-11T11:33:28.000Z (about 1 year ago)
- Last Synced: 2024-04-24T03:21:50.222Z (about 1 year ago)
- Topics: database, dibi, nette
- Language: PHP
- Homepage:
- Size: 44.9 MB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
- License: license.md
Awesome Lists containing this project
README
## Drago Database
Simple recurring questions.[](https://raw.githubusercontent.com/drago-ex/database/master/license.md)
[](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)## Technology
- PHP 8.3 or higher
- 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)