mezon /crud-service-model
具有CRUD操作的模型
1.2.23
2023-08-21 14:05 UTC
Requires
- php: >=7.2.0
- mezon/pdocrud: 1.6.*
- mezon/service-model: 1.2.*
Requires (Dev)
- phpunit/php-token-stream: 3.1.2
- phpunit/phpunit: ^8.5
- vimeo/psalm: ^4.2
README
此模型将帮助您为您的实体创建CRUD模型,并在简单设置后提供大量方法。
安装
只需在控制台打印
composer require mezon/crud-service-model
这就是全部了()
第一步
让我们为您的数据库实体定义一个新类
class EntityModel extends CrudServiceModel { /** * Constructor */ public function __construct() { parent::__construct('*', 'entity_table_name'); } }
就在这一行代码中
parent::__construct('*', 'entity_table_name');
我们指定需要从名为'entity_table_name'
的表中获取'*'
(所有字段)。
如果您只需要数据库中的某些字段,只需在第一个参数中列出它们即可
parent::__construct('id,field1,field2,field3', 'entity_table_name');
完成这些后,您将获得以下方法。
但在使用这些方法之前请注意,您需要满足某些命名约定的要求。
主键字段必须命名为id
。我将添加设置以使用其他名称。
某些方法需要字段creation_date
来获取新记录,例如。
您可以在表中添加字段domain_id
以实现开箱即用的多实例。
现在让我们看看可用的方法。
// Method fetches all new records since the $date // For example $model->newRecordsSince($domainId, '2021-01-01'); newRecordsSince($domainId, string $date): array;
// Method calculates count of records fetched by filter // For example $model->recordsCount(false, ['field1 = 1', 'field2 = 2']); recordsCount($domainId = false, array $where = ['1=1']);
// Method returns data as is without any transformations getSimpleRecords($domainId, int $from, int $limit, array $where, array $order = []): array;
// Method returns records wich are transformed by method getRecordsTransformer wuch // you can override in your subclass getRecords($domainId, int $from, int $limit, array $where = ['1=1'], array $order = []): array;
// Method returns the last $count records filtered by where lastRecords($domainId, $count, $where): array;
// Method returns records by their ids fetchRecordsByIds($domainId, string $ids): array;
// Method calculates count of records grouped by fieldName and filtered by where recordsCountByField($domainId, string $fieldName, array $where): array;
// Method updates records with values record updateBasicFields($domainId, array $record, array $where): array;
// Method inserts record record insertBasicFields(array $record, $domainId = 0): array;
// Method deletes all records filtered by where deleteFiltered($domainId, array $where): int;