phpci/PHPCI/Store/Base/ProjectStoreBase.php

103 lines
2.8 KiB
PHP
Raw Normal View History

2013-05-03 17:02:53 +02:00
<?php
/**
* Project base store for table: project
*/
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\Project;
2013-05-03 17:02:53 +02:00
/**
* Project Base Store
*/
class ProjectStoreBase extends Store
{
protected $tableName = 'project';
protected $modelName = '\PHPCI\Model\Project';
protected $primaryKey = 'id';
2013-05-03 17:02:53 +02:00
2013-05-16 17:03:34 +02:00
public function getByPrimaryKey($value, $useConnection = 'read')
{
return $this->getById($value, $useConnection);
}
2013-05-03 17:02:53 +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-03 17:02:53 +02:00
2014-02-25 17:43:06 +01:00
$query = 'SELECT * FROM `project` 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-03 17:02:53 +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 Project($data);
2013-05-16 17:03:34 +02:00
}
}
2013-05-16 17:03:34 +02:00
return null;
}
2013-10-10 02:01:06 +02:00
public function getByTitle($value, $limit = 1000, $useConnection = 'read')
2013-10-10 02:01:06 +02:00
{
if (is_null($value)) {
throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.');
}
$query = 'SELECT * FROM `project` WHERE `title` = :title LIMIT :limit';
$stmt = Database::getConnection($useConnection)->prepare($query);
2013-10-10 02:01:06 +02:00
$stmt->bindValue(':title', $value);
$stmt->bindValue(':limit', (int)$limit, \PDO::PARAM_INT);
2013-10-10 02:01:06 +02:00
if ($stmt->execute()) {
$res = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$map = function ($item) {
return new Project($item);
};
$rtn = array_map($map, $res);
$count = count($rtn);
2013-10-10 02:01:06 +02:00
return array('items' => $rtn, 'count' => $count);
} else {
return array('items' => array(), 'count' => 0);
}
}
2015-10-08 17:33:01 +02:00
public function getByGroupId($value, $limit = 1000, $useConnection = 'read')
{
if (is_null($value)) {
throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.');
}
$query = 'SELECT * FROM `project` WHERE `group_id` = :group_id LIMIT :limit';
$stmt = Database::getConnection($useConnection)->prepare($query);
$stmt->bindValue(':group_id', $value);
$stmt->bindValue(':limit', (int)$limit, \PDO::PARAM_INT);
if ($stmt->execute()) {
$res = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$map = function ($item) {
return new Project($item);
};
$rtn = array_map($map, $res);
$count = count($rtn);
return array('items' => $rtn, 'count' => $count);
} else {
return array('items' => array(), 'count' => 0);
}
}
2013-05-03 17:02:53 +02:00
}