add developper binding to platform
This commit is contained in:
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Model;
|
||||
|
||||
use Hyperf\Database\Model\Relations\BelongsTo;
|
||||
|
||||
/**
|
||||
* @mixin \App_Model_Platform
|
||||
@@ -20,10 +21,15 @@ class Platform extends Model
|
||||
/**
|
||||
* The attributes that are mass assignable.
|
||||
*/
|
||||
protected array $fillable = [];
|
||||
protected array $fillable = ['developer_id'];
|
||||
|
||||
/**
|
||||
* The attributes that should be cast to native types.
|
||||
*/
|
||||
protected array $casts = [];
|
||||
protected array $casts = ['developer_id' => 'integer'];
|
||||
|
||||
public function developer(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(User::class, 'developer_id');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ declare(strict_types=1);
|
||||
|
||||
namespace App\Model;
|
||||
|
||||
use Hyperf\Database\Model\Relations\HasMany;
|
||||
use Hyperf\DbConnection\Model\Model;
|
||||
use Qbhy\HyperfAuth\Authenticatable;
|
||||
|
||||
@@ -91,6 +92,20 @@ class User extends Model implements Authenticatable
|
||||
return password_verify($password, $this->password);
|
||||
}
|
||||
|
||||
public function developedPlatforms(): HasMany
|
||||
{
|
||||
return $this->hasMany(Platform::class, 'developer_id');
|
||||
}
|
||||
|
||||
protected static function boot(): void
|
||||
{
|
||||
parent::boot();
|
||||
|
||||
static::deleting(function (User $user) {
|
||||
$user->developedPlatforms()->update(['developer_id' => 1]);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if refresh token is valid.
|
||||
*/
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
use Hyperf\Database\Schema\Schema;
|
||||
use Hyperf\Database\Schema\Blueprint;
|
||||
use Hyperf\Database\Migrations\Migration;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*/
|
||||
public function up(): void
|
||||
{
|
||||
Schema::table('platforms', function (Blueprint $table) {
|
||||
$table->unsignedBigInteger('developer_id')->default(1)->comment('数据维护者,关联 users.id');
|
||||
$table->foreign('developer_id')->references('id')->on('users')->onDelete('restrict');
|
||||
$table->index('developer_id', 'idx_platforms_developer_id');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('platforms', function (Blueprint $table) {
|
||||
$table->dropForeign(['developer_id']);
|
||||
$table->dropIndex('idx_platforms_developer_id');
|
||||
$table->dropColumn('developer_id');
|
||||
});
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user