phpci/PHPCI/Store/Base/UserStoreBase.php

85 lines
2.2 KiB
PHP
Raw Normal View History

2013-05-10 17:25:51 +02:00
<?php
/**
* User base store for table: user
*/
namespace PHPCI\Store\Base;
2013-05-16 17:03:34 +02:00
2013-10-10 02:01:06 +02:00
use b8\Database;
use b8\Exception\HttpException;
2014-02-25 17:43:06 +01:00
use PHPCI\Store;
2013-10-10 02:01:06 +02:00
use PHPCI\Model\User;
2013-05-10 17:25:51 +02:00
/**
* User Base Store
*/
class UserStoreBase extends Store
{
protected $tableName = 'user';
protected $modelName = '\PHPCI\Model\User';
protected $primaryKey = 'id';
2013-05-10 17:25:51 +02:00
2013-05-16 17:03:34 +02:00
public function getByPrimaryKey($value, $useConnection = 'read')
{
return $this->getById($value, $useConnection);
}
2013-05-10 17:25:51 +02:00
2013-05-16 17:03:34 +02:00
public function getById($value, $useConnection = 'read')
{
if (is_null($value)) {
2013-10-10 02:01:06 +02:00
throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.');
2013-05-16 17:03:34 +02:00
}
2013-05-10 17:25:51 +02:00
2014-02-25 17:43:06 +01:00
$query = 'SELECT * FROM `user` WHERE `id` = :id LIMIT 1';
$stmt = Database::getConnection($useConnection)->prepare($query);
2013-05-16 17:03:34 +02:00
$stmt->bindValue(':id', $value);
2013-05-10 17:25:51 +02:00
2013-05-16 17:03:34 +02:00
if ($stmt->execute()) {
if ($data = $stmt->fetch(\PDO::FETCH_ASSOC)) {
2013-10-10 02:01:06 +02:00
return new User($data);
2013-05-16 17:03:34 +02:00
}
}
2013-05-16 17:03:34 +02:00
return null;
}
2013-05-10 17:25:51 +02:00
2013-05-16 17:03:34 +02:00
public function getByEmail($value, $useConnection = 'read')
{
if (is_null($value)) {
2013-10-10 02:01:06 +02:00
throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.');
2013-05-16 17:03:34 +02:00
}
2013-05-10 17:25:51 +02:00
2014-02-25 17:43:06 +01:00
$query = 'SELECT * FROM `user` WHERE `email` = :email LIMIT 1';
$stmt = Database::getConnection($useConnection)->prepare($query);
2013-05-16 17:03:34 +02:00
$stmt->bindValue(':email', $value);
2013-05-10 17:25:51 +02:00
2013-05-16 17:03:34 +02:00
if ($stmt->execute()) {
if ($data = $stmt->fetch(\PDO::FETCH_ASSOC)) {
2013-10-10 02:01:06 +02:00
return new User($data);
2013-05-16 17:03:34 +02:00
}
}
2013-05-16 17:03:34 +02:00
return null;
}
2015-10-08 17:52:19 +02:00
public function getByName($value, $useConnection = 'read')
{
if (is_null($value)) {
throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.');
}
2015-10-08 17:52:19 +02:00
$query = 'SELECT * FROM `user` WHERE `name` = :name LIMIT 1';
$stmt = Database::getConnection($useConnection)->prepare($query);
2015-10-08 17:52:19 +02:00
$stmt->bindValue(':name', $value);
if ($stmt->execute()) {
if ($data = $stmt->fetch(\PDO::FETCH_ASSOC)) {
return new User($data);
}
}
return null;
}
2013-05-10 17:25:51 +02:00
}