drago-ex / database
Connecting to database for Nette Framework
Installs: 4 114
Dependents: 4
Suggesters: 0
Security: 0
Stars: 1
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: >=8.1 <8.4
- dibi/dibi: ^5.0
Requires (Dev)
- nette/bootstrap: ^3.0
- nette/tester: ^2.3
- phpstan/phpstan-nette: ^1.2.9
- tracy/tracy: ^2.7
README
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->table();
Search for a record by column name in the table.
$this->model->table('email = ?', 'email@email.com');
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); }
Tips
You can also use entities and have them generated. https://github.com/drago-ex/generator