Switching from NetBean to Medoo
This commit is contained in:
54
src/Repositories/PostRepositoryMedoo.php
Normal file
54
src/Repositories/PostRepositoryMedoo.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Repositories;
|
||||
|
||||
use Medoo\Medoo;
|
||||
|
||||
/**
|
||||
* Repository pour "post" basé sur Medoo.
|
||||
* Retourne et consomme des tableaux associatifs.
|
||||
*/
|
||||
class PostRepositoryMedoo
|
||||
{
|
||||
private Medoo $db;
|
||||
|
||||
public function __construct(Medoo $db)
|
||||
{
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
public function allDesc(): array
|
||||
{
|
||||
return $this->db->select('post', ['id', 'title', 'content'], ['ORDER' => ['id' => 'DESC']]);
|
||||
}
|
||||
|
||||
public function find(int $id): ?array
|
||||
{
|
||||
$row = $this->db->get('post', ['id', 'title', 'content'], ['id' => $id]);
|
||||
return $row === null ? null : $row;
|
||||
}
|
||||
|
||||
public function create(array $data): int
|
||||
{
|
||||
$this->db->insert('post', [
|
||||
'title' => $data['title'] ?? '',
|
||||
'content' => $data['content'] ?? '',
|
||||
]);
|
||||
return (int)$this->db->id();
|
||||
}
|
||||
|
||||
public function update(int $id, array $data): void
|
||||
{
|
||||
$this->db->update('post', [
|
||||
'title' => $data['title'] ?? '',
|
||||
'content' => $data['content'] ?? '',
|
||||
], ['id' => $id]);
|
||||
}
|
||||
|
||||
public function delete(int $id): void
|
||||
{
|
||||
$this->db->delete('post', ['id' => $id]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user