42 lines
1.2 KiB
PHP
42 lines
1.2 KiB
PHP
<?php
|
|||
|
|
/**
|
||
|
|
* 家庭:数据空间的根,所有名册条目都挂在它下面。
|
||
|
|
*/
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace App;
|
||
|
|
|
||
|
|
final class Family
|
||
|
|
{
|
||
|
|
public static function find(int $id): ?array
|
||
|
|
{
|
||
|
|
$st = Database::get()->prepare('SELECT * FROM families WHERE id = ?');
|
||
|
|
$st->execute([$id]);
|
||
|
|
$row = $st->fetch();
|
||
|
|
return $row ?: null;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function create(string $name, string $motto = ''): int
|
||
|
|
{
|
||
|
|
$st = Database::get()->prepare('INSERT INTO families (name, motto) VALUES (?, ?)');
|
||
|
|
$st->execute([$name, $motto !== '' ? $motto : null]);
|
||
|
|
return (int)Database::get()->lastInsertId();
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function update(int $id, string $name, string $motto = ''): void
|
||
|
|
{
|
||
|
|
$st = Database::get()->prepare('UPDATE families SET name = ?, motto = ? WHERE id = ?');
|
||
|
|
$st->execute([$name, $motto !== '' ? $motto : null, $id]);
|
||
|
|
}
|
||
|
|
|
||
|
|
public static function countMembers(int $familyId): int
|
||
|
|
{
|
||
|
|
$st = Database::get()->prepare(
|
||
|
|
"SELECT COUNT(*) FROM persons WHERE family_id = ? AND kind = 'member'"
|
||
|
|
);
|
||
|
|
$st->execute([$familyId]);
|
||
|
|
return (int)$st->fetchColumn();
|
||
|
|
}
|
||
|
|
}
|