Bicycle/App/Repositories/GoalRepository.php
Egor Isaev ac1334c784 dev
2026-08-14 14:05:41 +03:00

55 lines
2.0 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 GoalRepository.php
* @copyright (c) 11/08/2026
*/
namespace App\Repositories;
use PDO;
use Services\DataBase\Model;
use System\Classes\Repository;
/**
* Репозиторий таблицы goals (цель для savings-счёта — необязательная надстройка).
*/
class GoalRepository extends Repository
{
/**
* @var string Таблица accounts для JOIN в getForUser() — вынесена в свойство (см. BudgetRepository::$categories_table).
*/
protected string $accounts_table = 'accounts';
/**
* @param string|null $connection Имя подключения (Database::instance($connection)); null — по умолчанию
*/
public function __construct(?string $connection = null)
{
parent::__construct('goals', connection: $connection);
}
/**
* Цели пользователя (по его активным счетам) вместе с данными счёта-копилки — для карточек
* «Копилки и цели» на дашборде.
*
* @param int $user_id
* @return array<int,object> Строки goals + account_title/account_summa/account_currency
*/
public function getForUser(int $user_id): array
{
$sql = "SELECT g.*, a.title AS account_title, a.summa AS account_summa, a.currency AS account_currency
FROM $this->table g
INNER JOIN $this->accounts_table a ON a.id = g.account_id
WHERE a.user_id = :user_id AND a.is_delete = 0";
$stmt = $this->pdo->prepare($sql);
$stmt->execute(['user_id' => $user_id]);
// $this->obj_class — private в Repository (не protected), сюда не дотянуться из наследника;
// Model — и так дефолт конструктора Repository, GoalRepository его не переопределяет.
return $stmt->fetchAll(PDO::FETCH_CLASS, Model::class);
}
}