dbms_primeidc/extdbms/lib/Services/CommonService.php
2025-04-11 11:54:50 +09:00

75 lines
2.1 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 protected 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;
}
public function insert(array $formData): int
{
$insertId = $this->getModel()->insert($formData);
if (!$insertId) {
throw new \Exception("Insert Error : " . $this->getModel()->getLastError());
}
return $insertId;
} //
//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