43 lines
1.5 KiB
PHP
43 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Repositories;
|
|
|
|
final class PlayerRepository extends BaseRepository
|
|
{
|
|
public function all(): array
|
|
{
|
|
$params = [];
|
|
$sql = 'SELECT players.*, teams.name AS team_name
|
|
FROM players JOIN teams ON teams.id = players.team_id';
|
|
if (!empty($_GET['team_id'])) {
|
|
$sql .= ' WHERE team_id = :team_id';
|
|
$params['team_id'] = (int) $_GET['team_id'];
|
|
}
|
|
$sql .= ' ORDER BY players.last_name, players.first_name';
|
|
return $this->paginate($sql, $params);
|
|
}
|
|
|
|
public function create(array $data): array
|
|
{
|
|
$stmt = $this->db->prepare(
|
|
'INSERT INTO players (team_id, first_name, last_name, document_id, birth_date, jersey_number, position, photo_path)
|
|
VALUES (:team_id, :first_name, :last_name, :document_id, :birth_date, :jersey_number, :position, :photo_path)'
|
|
);
|
|
$stmt->execute([
|
|
'team_id' => $data['team_id'],
|
|
'first_name' => $data['first_name'],
|
|
'last_name' => $data['last_name'],
|
|
'document_id' => $data['document_id'],
|
|
'birth_date' => $data['birth_date'] ?? null,
|
|
'jersey_number' => $data['jersey_number'] ?? null,
|
|
'position' => $data['position'] ?? null,
|
|
'photo_path' => $data['photo_path'] ?? null,
|
|
]);
|
|
|
|
$id = (int) $this->db->lastInsertId();
|
|
$stmt = $this->db->prepare('SELECT * FROM players WHERE id = :id');
|
|
$stmt->execute(['id' => $id]);
|
|
return $stmt->fetch();
|
|
}
|
|
}
|