90 lines
2.7 KiB
PHP
90 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace lib\Services;
|
|
|
|
use lib\Core\Service as Core;
|
|
|
|
abstract class CommonService extends Core
|
|
{
|
|
private $_model = null;
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
} //
|
|
abstract public function getModelClass(): string;
|
|
abstract public function getEntityClass(): string;
|
|
final public function getModel(): mixed
|
|
{
|
|
if ($this->_model === null) {
|
|
$modelClass = $this->getModelClass();
|
|
$this->_model = new $modelClass();
|
|
// $this->_model->setDebug(true);
|
|
}
|
|
return $this->_model;
|
|
}
|
|
final public function getEntity(): mixed
|
|
{
|
|
$result = $this->getModel()->first();
|
|
if (!$result) { //결과값이 없으면 null
|
|
return $result;
|
|
}
|
|
$entityClass = $this->getEntityClass();
|
|
return new $entityClass($result);
|
|
}
|
|
final public function getEntities(): array
|
|
{
|
|
$entitys = [];
|
|
foreach ($this->getModel()->get() as $result) {
|
|
$entityClass = $this->getEntityClass();
|
|
$entity = new $entityClass($result);
|
|
$pairField = $this->getModel()->getPairField();
|
|
$entitys[$entity->$pairField] = $entity;
|
|
}
|
|
return $entitys;
|
|
} //
|
|
final public function getCount(string $select = "COUNT(*) as cnt"): int
|
|
{
|
|
$count = $this->getModel()->count($select);
|
|
// echo "<BR>" . $this->getModel()->getLastQuery();
|
|
return $count;
|
|
}
|
|
final public function getList(int $curPage = 1, int $perPage = APP_VIEW_LIST_PERPAGE): array
|
|
{
|
|
//Query문 Rest여부 -> 같은조건에 Count 받고, 결과값을 받고 싶을때는 continue()
|
|
$this->getModel()->setContinue(true);
|
|
$total = $this->getCount();
|
|
//limit, offset 설정
|
|
$this->getModel()->limit($perPage);
|
|
$this->getModel()->offset(($curPage - 1) * $perPage);
|
|
$entities = $this->getEntities();
|
|
return [$total, $entities];
|
|
}
|
|
|
|
protected function insert(array $formData): mixed
|
|
{
|
|
$insertId = $this->getModel()->insert($formData);
|
|
if (!$insertId) {
|
|
throw new \Exception("Insert Error : " . $this->getModel()->getLastError());
|
|
}
|
|
$entityClass = $this->getEntityClass();
|
|
$entity = new $entityClass($formData);
|
|
$pkField = $this->getModel()::PKFIELD;
|
|
$entity->$pkField = $insertId;
|
|
return $entity;
|
|
} //
|
|
|
|
//transaction관련련
|
|
final public function beginTransaction(): void
|
|
{
|
|
$this->getModel()->beginTransaction();
|
|
}
|
|
final public function commit(): void
|
|
{
|
|
$this->getModel()->commit();
|
|
}
|
|
final public function rollBack(): void
|
|
{
|
|
$this->getModel()->rollBack();
|
|
}
|
|
} //Class
|