servermgrv2/app/Models/CommonTrait.php
2023-07-17 21:09:49 +09:00

42 lines
1.2 KiB
PHP

<?php
namespace App\Models;
use App\Libraries\Log\Log;
trait CommonTrait
{
private function setEntityDatas_CommonTrait($entity, array $datas)
{
foreach ($this->allowedFields as $field) {
if ($entity->$field != $datas[$field]) {
$entity->$field = $datas[$field];
}
}
return $entity;
}
protected function create_CommonTrait($entity, array $datas)
{
$entity = $this->setEntityDatas_CommonTrait($entity, $datas);
if (!$this->save($entity)) {
Log::add("error", __FUNCTION__ . "에서 호출:" . $this->getLastQuery());
Log::add("error", implode("\n", $this->errors()));
throw new \Exception(__FUNCTION__ . " 오류 발생.\n" . var_export($this->errors(), true));
}
$entity->$this->primaryKey = $this->insertID();
return $entity;
}
protected function modify_CommonTrait($entity, array $datas)
{
$entity = $this->setEntityDatas_CommonTrait($entity, $datas);
if ($entity->hasChanged()) {
if (!$this->save($entity)) {
Log::add("error", __FUNCTION__ . "에서 호출:" . $this->getLastQuery());
Log::add("error", implode("\n", $this->errors()));
throw new \Exception(__FUNCTION__ . " 오류 발생.\n" . var_export($this->errors(), true));
}
}
return $entity;
}
}