chg: dev: add RecentBattle entity that is a Materialized View to speed up the view - and further refactor on ProfileController #8

This commit is contained in:
Lang
2026-04-20 21:08:15 +02:00
parent 6a5ba84b5e
commit 2ec37a802b
7 changed files with 341 additions and 35 deletions
+63
View File
@@ -0,0 +1,63 @@
<?php declare(strict_types=1);
/**
* This file is part of the SplendidBear Websites' projects.
*
* Copyright (c) 2026 @ www.splendidbear.org
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace App\Repository;
use App\Entity\RecentBattle;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\DBAL\Exception;
use Doctrine\Persistence\ManagerRegistry;
use RuntimeException;
/**
* Class RecentBattleRepository
*
* @package App\Repository
* @author Lang <https://www.splendidbear.org>
* @category Class
* @license https://www.gnu.org/licenses/lgpl-3.0.en.html GNU Lesser General Public License
* @link www.splendidbear.org
* @since 2026. 04. 20.
*
* @method RecentBattle|null find($id, $lockMode = null, $lockVersion = null)
* @method RecentBattle|null findOneBy(array $criteria, array $orderBy = null)
* @method RecentBattle[] findAll()
* @method RecentBattle[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class RecentBattleRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, RecentBattle::class);
}
public function findRecentForUser(int $userId, int $limit = 30): array
{
return $this->createQueryBuilder('rb')
->where('rb.userId = :uid')
->setParameter('uid', $userId)
->orderBy('rb.updated', 'DESC')
->setMaxResults($limit)
->getQuery()
->getResult();
}
public function refreshMaterializedView(): void
{
try {
$this
->getEntityManager()
->getConnection()
->executeStatement('REFRESH MATERIALIZED VIEW CONCURRENTLY recent_battles');
} catch (Exception $e) {
throw new RuntimeException("Failed to refresh materialized view: {$e->getMessage()}", 0, $e);
}
}
}