1. Go to this page and download the library: Download mspirkov/yii2-db library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mspirkov / yii2-db example snippets
use MSpirkov\Yii2\Db\ActiveRecord\RepositoryInterface;
/**
* @extends RepositoryInterface<Product>
*/
interface ProductRepositoryInterface extends RepositoryInterface
{
/**
* @return Product[]
*/
public function findForMainPage(int $limit): array
}
use MSpirkov\Yii2\Db\ActiveRecord\AbstractRepository;
/**
* @extends AbstractRepository<Product>
*/
final class ProductRepository extends AbstractRepository implements ProductRepositoryInterface
{
public function __construct()
{
parent::__construct(Product::class);
}
public function findForMainPage(int $limit): array
{
return $this->find()
->where(['hidden' => 0])
->orderBy(['id' => SORT_DESC])
->limit($limit)
->all();
}
}
final readonly class MainService
{
private const int PRODUCTS_LIMIT = 20;
public function __construct(
private ProductRepositoryInterface $productRepository,
) {}
/**
* @return array{
* products: Product[],
* }
*/
public function getMainData(int $id): array
{
$products = $this->productRepository->findForMainPage(self::PRODUCTS_LIMIT);
return [
'products' => $products,
];
}
}
use MSpirkov\Yii2\Db\ActiveRecord\DateTimeBehavior;
/**
* @property int $id
* @property string $content
* @property string $created_at
* @property string|null $updated_at
*/
final class Message extends ActiveRecord
{
public static function tableName(): string
{
return '{{messages}}';
}
public function behaviors(): array
{
return [
DateTimeBehavior::class,
];
}
}
use MSpirkov\Yii2\Db\ActiveRecord\DateTimeBehavior;
use yii\db\Expression;
/**
* @property int $id
* @property string $content
* @property string $create_time
* @property string|null $update_time
*/
final class Message extends ActiveRecord
{
public static function tableName(): string
{
return '{{messages}}';
}
public function behaviors(): array
{
return [
[
'class' => DateTimeBehavior::class,
'createdAtAttribute' => 'create_time',
'updatedAtAttribute' => 'update_time',
'value' => new Expression('NOW()'),
],
];
}
}
use MSpirkov\Yii2\Db\TransactionManagerInterface;
use MSpirkov\Yii2\Db\TransactionManager;
return [
...
'container' => [
'definitions' => [
TransactionManagerInterface::class => static fn() => new TransactionManager(Yii::$app->db),
],
],
...
];
use MSpirkov\Yii2\Db\TransactionManagerInterface;
final readonly class ProductService
{
public function __construct(
private TransactionManagerInterface $transactionManager,
private FilesystemInterface $filesystem,
private ProductRepositoryInterface $productRepository,
) {}
/**
* @return array{success: bool, message?: string}
*/
public function deleteProduct(int $id): array
{
$product = $this->productRepository->findOne($id);
// There's some logic here. For example, checking for the existence of a product.
$transactionResult = $this->transactionManager->safeWrap(function () use ($product) {
$this->productRepository->delete($product);
$this->filesystem->delete($product->file_path);
return [
'success' => true,
];
});
if ($transactionResult === false) {
return [
'success' => false,
'message' => 'Something went wrong',
];
}
return $transactionResult;
}
}
bash
php composer.phar
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.