Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
July 27, 2021 04:58 am GMT

CRUD model class

CRUD model

This model will help you to create CRUD models for your entitites, and provides you a huge set of methods after a simpe setup.

Installation

Just print in console

composer require mezon/crud-service-model

And that's all )

First steps

Let's define a new class for your DB entity:

class EntityModel extends CrudServiceModel{    /**     * Constructor     */    public function __construct()    {        parent::__construct('*', 'entity_table_name');    }}

In this exact line of code:

parent::__construct('*', 'entity_table_name');

We have specified that we need '*' (all fields) from the table with name 'entity_table_name'.

If you need only some fields from your database, just list them in the first parameter:

parent::__construct('id,field1,field2,field3', 'entity_table_name');

And when you have done this, you will get the following methods.

But before using these methods note that you will need to meet the requirements for some naming conventions.

Field with the primary key must be named as id. I shall add setting to use another name.

Some methods reqire field creation_date for fetching new records for example.

You may add to the table field domain_id for implementing multy instancing out of the box.

Now lets look at available methods:

// 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 transformationsgetSimpleRecords($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 subclassgetRecords($domainId, int $from, int $limit, array $where = ['1=1'], array $order = []): array;
// Method returns the last $count records filtered by wherelastRecords($domainId, $count, $where): array;
// Method returns records by their idsfetchRecordsByIds($domainId, string $ids): array;
// Method calculates count of records grouped by fieldName and filtered by whererecordsCountByField($domainId, string $fieldName, array $where): array;
// Method updates records with values recordupdateBasicFields($domainId, array $record, array $where): array;
// Method inserts record recordinsertBasicFields(array $record, $domainId = 0): array;
// Method deletes all records filtered by wheredeleteFiltered($domainId, array $where): int;

It will be great if you will contribute something to this project. Documentation, sharing the project in your social media, bug fixing, refactoring, or even submitting issue with question or feature request. Thanks anyway )


Original Link: https://dev.to/alexdodonov/crud-model-class-5an1

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To