Bicycle/App/Repositories/AccountRepository.php
2026-08-15 19:40:29 +03:00

44 lines
1.6 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
/**
* @package Bicycle
* @author Egor Isaev
* @description AccountRepository.php
* @copyright (c) 11/08/2026
*/
namespace App\Repositories;
use System\Classes\Repository;
/**
* Репозиторий таблицы accounts (счета: cash/bank/savings).
*/
class AccountRepository extends Repository
{
/**
* @param string|null $connection Имя подключения (Database::instance($connection)); null — по умолчанию
*/
public function __construct(?string $connection = null)
{
parent::__construct('accounts', connection: $connection);
}
/**
* Изменить баланс счёта на $delta (может быть отрицательным) — читает текущий `summa` и
* перезаписывает, а не `summa = summa + :delta` прямо в SQL, чтобы остаться на общем
* processData()/update() репозитория (bind-параметры, автоопределение типа), как и весь
* остальной код Repository. Вызывающая сторона (App\Controller\TransactionsController)
* оборачивает это в `$repo->transaction()` вместе с самой операцией — атомарность.
*
* @param int $id
* @param float $delta
* @return void
*/
public function adjustBalance(int $id, float $delta): void
{
$account = $this->get($id);
$this->update(['id' => $id, 'summa' => (float)$account->summa + $delta]);
}
}