phpci/PHPCI/Store/Base/ProjectStoreBase.php

81 lines
2 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 = null, $useConnection = 'read')
{
if (is_null($value)) {
throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.');
}
$add = '';
2013-10-10 02:01:06 +02:00
if ($limit) {
$add .= ' LIMIT ' . $limit;
}
2013-10-10 02:01:06 +02:00
2014-02-25 17:43:06 +01:00
$query = 'SELECT * FROM `project` WHERE `title` = :title' . $add;
$stmt = Database::getConnection($useConnection)->prepare($query);
2013-10-10 02:01:06 +02:00
$stmt->bindValue(':title', $value);
if ($stmt->execute()) {
$res = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$map = function ($item) {
return new Project($item);
};
$rtn = array_map($map, $res);
2014-12-02 17:26:55 +01:00
$count = count($rtn);
2013-10-10 02:01:06 +02:00
return array('items' => $rtn, 'count' => $count);
} else {
return array('items' => array(), 'count' => 0);
}
}
2013-05-03 17:02:53 +02:00
}