drago-ex / database
连接Nette框架的数据库
v1.2.0
2024-01-24 05:42 UTC
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
常见问题。
技术
- PHP 8.1 或更高版本
- composer
知识
安装
composer require drago-ex/database
使用
#[Table('table', 'id')] class Model {}
在存储库中的基本查询
从表中获取记录。
$this->model->table();
在表中按列名搜索记录。
$this->model->table('email = ?', 'email@email.com');
按id搜索记录。
$this->model->get(1);
从数据库中删除记录。
$this->model->remove(1);
保存记录(如果添加了id列,则执行更新)。
$this->model->put(['column' => 'record']);
实体使用
class SampleEntity extends Drago\Database\Entity { public const Table = 'table'; public const PrimaryKey = 'id'; public ?int $id = null; public string $sample; }
基本存储库。
#[Table(SampleEntity::Table, SampleEntity::PrimarKey)] class Repository {}
在存储库中使用实体。
function find(int $id): array|SampleEntity|null { return $this->get($id)->fetch(); }
读取数据。
$row = $this->find(1); echo $row->id; echo $row->sample;
跨实体保存记录(添加id以更新记录)。
$entity = new SampleEntity; $entity->id = 1; $entity->sample = 'sample'; $this->save($entity);
保存方法将记录保存到数据库中。
function save(SampleEntity $entity): Result|int|null { return $this->put($entity); }
提示
您还可以使用实体并生成它们。 https://github.com/drago-ex/generator