Download the PHP package mspirkov/yii2-db without Composer
On this page you can find all versions of the php package mspirkov/yii2-db. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mspirkov/yii2-db
More information about mspirkov/yii2-db
Files in mspirkov/yii2-db
Informations about the package yii2-db
Yii2 DB Extension
A package of helper classes for working with databases in Yii2.
Support
If you like this project, give it a ⭐ on GitHub — it helps others discover it.
Installation
Run
or add
to the require section of your composer.json file.
Components
- AbstractRepository
- DateTimeBehavior
- TransactionManager
AbstractRepository
An abstract class for creating repositories that interact with ActiveRecord models.
Contains the most commonly used methods:
findOne- finds a single ActiveRecord model based on the provided condition.findAll- finds all ActiveRecord models based on the provided condition.save- saves an ActiveRecord model to the database.delete- deletes an ActiveRecord model from the database.updateAll- updates the whole table using the provided attribute values and conditions.deleteAll- deletes rows in the table using the provided conditions.
It also has several additional methods:
findOneWith- finds a single ActiveRecord model based on the provided condition and eager loads the specified relations.findAllWith- finds all ActiveRecord models based on the provided condition and eager loads the specified relations.getTableSchema- returns the schema information of the DB table associated with current ActiveRecord class.find- creates and returns a new ActiveQuery instance for the current ActiveRecord model.
This way, you can separate the logic of executing queries from the ActiveRecord models themselves. This will make your ActiveRecord models thinner and simpler. It will also make testing easier, as you can mock the methods for working with the database.
Usage example
Create an interface based on RepositoryInterface:
Next, create your repository:
After that, specify the implementation of the ProductRepositoryInterface interface in the container in the definitions section:
After that, you can use the repository as follows:
DateTimeBehavior
Behavior for ActiveRecord models that automatically fills the specified attributes with the current date and time.
Usage example
By default, this behavior will fill the created_at attribute with the date and time when the associated
AR object is being inserted; it will fill the updated_at attribute with the date and time when the AR object
is being updated. The date and time are determined relative to $timeZone.
If your attribute names are different or you want to use a different way of calculating the timestamp,
you may configure the $createdAtAttribute, $updatedAtAttribute and $value properties like the following:
TransactionManager
A utility class for managing database transactions with a consistent and safe approach.
This class simplifies the process of wrapping database operations within transactions, ensuring that changes are either fully committed or completely rolled back in case of errors.
It provides two main methods:
safeWrap- executes a callable within a transaction, safely handling exceptions and logging them.wrap- executes a callable within a transaction.
Usage example
Initialization
Add the definition to the container configuration in the definitions section: