User repository

pull/8/head
sundowndev 2018-07-22 02:33:49 +02:00
parent b561b284a5
commit a1f99892af
1 changed files with 74 additions and 0 deletions

View File

@ -0,0 +1,74 @@
<?php
namespace App\Repository;
/**
* Class TaskRepository
* @package App\Repository
*/
class UserRepository
{
/**
* @var Database
*/
private $db;
/**
* @var
*/
private $tableName;
/**
* TaskRepository constructor.
* @param $db
*/
public function __construct($db)
{
$this->db = $db;
$this->tableName = 'User';
}
/**
* @return mixed
*/
public function findAll()
{
$stmt = $this->db->getConnection()->prepare('SELECT * FROM ' . $this->tableName . ' ORDER BY id DESC');
$stmt->execute();
return $stmt->fetchAll(\PDO::FETCH_ASSOC);
}
/**
* @param $id
* @return null
*/
public function findOneById($id)
{
$stmt = $this->db->getConnection()->prepare('SELECT * FROM ' . $this->tableName . ' WHERE id = :id');
$stmt->bindParam(':id', $id, \PDO::PARAM_INT);
$stmt->execute();
$user = $stmt->fetch(\PDO::FETCH_ASSOC);
if (!$user) {
return null;
} else {
return $user;
}
}
public function findOneByUsername($username)
{
$stmt = $this->db->getConnection()->prepare('SELECT * FROM ' . $this->tableName . ' WHERE name = :username');
$stmt->bindParam(':username', $username, \PDO::PARAM_INT);
$stmt->execute();
$user = $stmt->fetch(\PDO::FETCH_ASSOC);
if (!$user) {
return null;
} else {
return $user;
}
}
}