Slide 121
Slide 121 text
class UserAccountRepo extends DB {
public function getByEmail(string $email): ?UserAccount {
$pdo = static::getPdo();
$stmt = $pdo->prepare("SELECT * FROM `user_account` WHERE email = :email");
$stmt->bindValue('email', $email, \PDO::PARAM_STR);
$stmt->execute();
$stmt->setFetchMode(\PDO::FETCH_CLASS, '\MyApp\Model\UserAccount');
return $stmt->fetch() ?: null;
}
public function create(UserAccount $ua): int {
if(!$ua->isValid()) throw new \InvalidArgumentException("invalid user account model");
$pdo = static::getPdo();
$stmt = $pdo->prepare("
INSERT INTO user_account
(email, hashed_password, name) VALUES
(:email, :hashed_password, :name)
");
$stmt->bindValue('hashed_password', $ua->hashed_password, \PDO::PARAM_STR);
$stmt->bindValue('name', $ua->name, \PDO::PARAM_STR);
$stmt->bindValue('email', $ua->email, \PDO::PARAM_STR);
$result = $stmt->execute();
if (!$result) throw new \RuntimeException("DB query error.");
return $pdo->lastInsertId('id');
}