phpci/PHPCI/Store/Base/BuildStoreBase.php

142 lines
3.7 KiB
PHP
Raw Normal View History

2013-05-03 17:02:53 +02:00
<?php
/**
* Build base store for table: build
*/
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\Build;
2013-05-03 17:02:53 +02:00
/**
* Build Base Store
*/
class BuildStoreBase extends Store
{
protected $tableName = 'build';
protected $modelName = '\PHPCI\Model\Build';
protected $primaryKey = 'id';
2013-05-16 17:03:34 +02:00
2015-02-16 12:20:18 +01:00
/**
* Returns a Build model by primary key.
2015-05-31 16:01:51 +02:00
*
2015-02-16 12:20:18 +01:00
* @param mixed $value
* @param string $useConnection
2015-05-31 16:01:51 +02:00
*
2015-02-16 12:20:18 +01:00
* @return \@appNamespace\Model\Build|null
*/
2013-05-16 17:03:34 +02:00
public function getByPrimaryKey($value, $useConnection = 'read')
{
return $this->getById($value, $useConnection);
}
2015-02-16 12:20:18 +01:00
/**
* Returns a Build model by Id.
2015-05-31 16:01:51 +02:00
*
2015-02-16 12:20:18 +01:00
* @param mixed $value
* @param string $useConnection
2015-05-31 16:01:51 +02:00
*
2015-02-16 12:20:18 +01:00
* @return \@appNamespace\Model\Build|null
2015-05-31 16:01:51 +02:00
*
* @throws HttpException
2015-02-16 12:20:18 +01: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
}
2014-02-25 17:43:06 +01:00
$query = 'SELECT * FROM `build` WHERE `id` = :id LIMIT 1';
$stmt = Database::getConnection($useConnection)->prepare($query);
2013-05-16 17:03:34 +02:00
$stmt->bindValue(':id', $value);
if ($stmt->execute()) {
if ($data = $stmt->fetch(\PDO::FETCH_ASSOC)) {
2013-10-10 02:01:06 +02:00
return new Build($data);
2013-05-16 17:03:34 +02:00
}
}
2013-05-16 17:03:34 +02:00
return null;
}
2015-02-16 12:20:18 +01:00
/**
* Returns an array of Build models by ProjectId.
2015-05-31 16:01:51 +02:00
*
2015-02-16 12:20:18 +01:00
* @param mixed $value
* @param int $limit
* @param string $useConnection
2015-05-31 16:01:51 +02:00
*
2015-02-16 12:20:18 +01:00
* @throws HttpException
2015-05-31 16:01:51 +02:00
*
2015-02-16 12:20:18 +01:00
* @return array
*/
public function getByProjectId($value, $limit = 1000, $useConnection = 'read')
2013-05-16 17:03:34 +02:00
{
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
}
$query = 'SELECT * FROM `build` WHERE `project_id` = :project_id LIMIT :limit';
$stmt = Database::getConnection($useConnection)->prepare($query);
2013-05-16 17:03:34 +02:00
$stmt->bindValue(':project_id', $value);
$stmt->bindValue(':limit', (int)$limit, \PDO::PARAM_INT);
2013-05-16 17:03:34 +02:00
if ($stmt->execute()) {
$res = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$map = function ($item) {
2013-10-10 02:01:06 +02:00
return new Build($item);
2013-05-16 17:03:34 +02:00
};
$rtn = array_map($map, $res);
$count = count($rtn);
2013-05-16 17:03:34 +02:00
return array('items' => $rtn, 'count' => $count);
} else {
return array('items' => array(), 'count' => 0);
}
}
2015-02-16 12:20:18 +01:00
/**
* Returns an array of Build models by Status.
2015-05-31 16:01:51 +02:00
*
2015-02-16 12:20:18 +01:00
* @param mixed $value
* @param int $limit
* @param string $useConnection
2015-05-31 16:01:51 +02:00
*
2015-02-16 12:20:18 +01:00
* @return array
2015-05-31 16:01:51 +02:00
*
* @throws HttpException
2015-02-16 12:20:18 +01:00
*/
public function getByStatus($value, $limit = 1000, $useConnection = 'read')
2013-05-16 17:03:34 +02:00
{
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
}
$query = 'SELECT * FROM `build` WHERE `status` = :status LIMIT :limit';
$stmt = Database::getConnection($useConnection)->prepare($query);
2013-05-16 17:03:34 +02:00
$stmt->bindValue(':status', $value);
$stmt->bindValue(':limit', (int)$limit, \PDO::PARAM_INT);
2013-05-16 17:03:34 +02:00
if ($stmt->execute()) {
$res = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$map = function ($item) {
2013-10-10 02:01:06 +02:00
return new Build($item);
2013-05-16 17:03:34 +02:00
};
$rtn = array_map($map, $res);
$count = count($rtn);
2013-05-16 17:03:34 +02:00
return array('items' => $rtn, 'count' => $count);
} else {
return array('items' => array(), 'count' => 0);
}
}
2013-05-03 17:02:53 +02:00
}