Drago Database
Simple recurring questions.
Technology
- PHP 8.1 or higher
- composer
Knowledge
Installation
composer require drago-ex/database
Use
#[Table('table', 'id')]
class Model {}
Basic queries in the Repository
Get records from table.
$this->model->query();
Search for a record by column name in the table.
$this->model->query('email', 'email@email.com');
Get records by table name.
$this->model->queryOf('table');
Search for a record by id.
$this->model->get(1);
Delete a record from the database.
$this->model->remove(1);
Save record (the update will be performed if a column with id is added).
$this->model->put(['column' => 'record']);
Use of entity
class SampleEntity extends Drago\Database\Entity
{
public const Table = 'table';
public const PrimaryKey = 'id';
public ?int $id = null;
public string $sample;
}
Basic repository.
#[Table(SampleEntity::Table, SampleEntity::PrimarKey)]
class Repository {}
Use of an entity in a repository.
function find(int $id): array|SampleEntity|null
{
return $this->get($id)->fetch();
}
Reading data.
$row = $this->find(1);
echo $row->id;
echo $row->sample;
Save records across an entity (to update the record we add id).
$entity = new SampleEntity;
$entity->id = 1;
$entity->sample = 'sample';
$this->save($entity);
The save method saves the record to the database.
function save(SampleEntity $entity): Result|int|null
{
return $this->put($entity->toArray());
}
Tips
You can also use entities and have them generated. https://github.com/drago-ex/generator