Browse Source

更新者作成者の型を修正

develop
sosuke.iwabuchi 2 years ago
parent
commit
e85c3e572a
5 changed files with 81 additions and 13 deletions
  1. +0
    -11
      app/Listeners/Model/ModelListener.php
  2. +10
    -0
      app/Listeners/Model/UpdatingListener.php
  3. +16
    -1
      app/Models/AppModel.php
  4. +8
    -1
      app/Models/Feature/IModelFeature.php
  5. +47
    -0
      database/migrations/2023_09_04_155700_createdby_updatedby_change_to_uuid.php

+ 0
- 11
app/Listeners/Model/ModelListener.php View File

@@ -17,9 +17,6 @@ abstract class ModelListener
{
// 履歴作成処理
$this->createHistory($model);

// 更新者作成者設定処理
$this->setHandlerIds();
}

protected function createHistory(IModelFeature $model)
@@ -48,12 +45,4 @@ abstract class ModelListener
$changeMessage
));
}

protected function setHandlerIds()
{
if (Auth::check()) {
$this->created_by = Auth::id();
$this->updated_by = Auth::id();
}
}
}

+ 10
- 0
app/Listeners/Model/UpdatingListener.php View File

@@ -3,6 +3,7 @@
namespace App\Listeners\Model;

use App\Events\Model\UpdatingEvent;
use Illuminate\Support\Facades\Auth;

class UpdatingListener extends ModelListener
{
@@ -10,6 +11,15 @@ class UpdatingListener extends ModelListener

public function handle(UpdatingEvent $event): void
{
// 更新者作成者の設定
if (Auth::check()) {
$id = Auth::id();
$event->model->updated_by = $id;
if ($event->model->created_by === null) {
$event->model->created_by = $id;
}
}

$this->handleEvent($event->model);
}
}

+ 16
- 1
app/Models/AppModel.php View File

@@ -2,8 +2,10 @@

namespace App\Models;

use Exception;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;

abstract class AppModel extends BaseModel
@@ -13,7 +15,20 @@ abstract class AppModel extends BaseModel
public function getHistory(): ?HistoryModel
{
$historyName = static::class . 'History';
return new $historyName;


$history = new $historyName;

if ($history instanceof HistoryModel) {
if (Auth::check()) {
$id = Auth::id();
$history->updated_by = $id;
$history->created_by = $id;
}
return $history;
} else {
throw new Exception("履歴モデル不正");
}
}

public function getChangeLogMessage($before, $after): ?string


+ 8
- 1
app/Models/Feature/IModelFeature.php View File

@@ -4,7 +4,14 @@ namespace App\Models\Feature;

use App\Models\HistoryModel;
use Illuminate\Database\Query\Builder;

use Illuminate\Support\Carbon;

/**
* @property ?Carbon $updated_at
* @property ?Carbon $created_at
* @property ?string $updated_by
* @property ?string $created_by
*/
interface IModelFeature
{



+ 47
- 0
database/migrations/2023_09_04_155700_createdby_updatedby_change_to_uuid.php View File

@@ -0,0 +1,47 @@
<?php

use App\Util\MigrationHelper;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
$tables = Schema::getAllTables();

foreach ($tables as $table) {

$tableName = $table->tablename;
if (!Schema::hasColumn($tableName, "created_by") || !Schema::hasColumn($tableName, "updated_by")) continue;

$type = Schema::getColumnType($tableName, "created_by");

if (!in_array($type, ['uuid', 'guid'])) {
Schema::table($tableName, function (Blueprint $table) {
$table->dropColumn("created_by");
$table->dropColumn("updated_by");
});
Schema::table($tableName, function (Blueprint $table) {
$table->uuid("created_by")->nullable()->comment("作成者ID");;
$table->uuid("updated_by")->nullable()->comment("更新者ID");;
});
}
}
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
}
};

Loading…
Cancel
Save