Commit 448d29b1 authored by Antoine Le Gonidec's avatar Antoine Le Gonidec

Use a dedicated database table to store archive/script relationships

parent e1c3d83e
......@@ -8,14 +8,14 @@ class Archive extends Model {
*
* @var array
*/
protected $appends = [ 'archives', 'archive', 'script' ];
protected $appends = [ 'archives', 'archive', 'scripts' ];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [ 'archive_id', 'script_id' ];
protected $hidden = [ 'archive_id' ];
/**
* Get the archive that requires the current archive
......@@ -35,20 +35,25 @@ class Archive extends Model {
}
/**
* Get the script that supports the current archive
* Get the scripts that supports the current archive
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function script() {
return $this->BelongsTo(Script::class);
public function scripts() {
return $this->BelongsToMany(Script::class);
}
public function getScriptAttribute() {
$script = $this->script()->first();
if ( $script ) {
$script->makeHidden([ 'archives' ]);
}
return $script;
/**
* Hide scripts $archives attribute when shown from an archive
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getScriptsAttribute() {
$scripts = $this->scripts()->get();
$scripts->each(function ($script) {
$script->makeHidden([ 'archives', 'pivot' ]);
});
return $scripts;
}
/**
......
......@@ -16,15 +16,20 @@ class Script extends Model {
* @return \Illuminate\Database\Eloquent\Relations\HasMany
*/
public function archives() {
return $this->hasMany(Archive::class);
return $this->BelongsToMany(Archive::class);
}
/**
* Hide archives $script and $archive attributes when shown from a script
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getArchivesAttribute() {
$collection = $this->archives()->get();
$collection->each(function ($item) {
$item->makeHidden([ 'archive', 'script' ]);
$archives = $this->archives()->get();
$archives->each(function ($archive) {
$archive->makeHidden([ 'archive', 'scripts', 'pivot' ]);
});
return $collection;
return $archives;
}
/**
......
......@@ -11,9 +11,17 @@ class AddRelationshipBetweenArchivesAndScripts extends Migration {
* @return void
*/
public function up() {
Schema::table('archives', function (Blueprint $table) {
$table->integer('script_id')->unsigned()->nullable();
$table->foreign('script_id')->references('id')->on('scripts');
Schema::create('archive_script', function (Blueprint $table) {
$table->unsignedInteger('script_id');
$table->foreign('script_id')
->references('id')
->on('scripts')
->onDelete('cascade');
$table->unsignedInteger('archive_id');
$table->foreign('archive_id')
->references('id')
->on('archives')
->onDelete('cascade');
});
}
......@@ -23,9 +31,6 @@ class AddRelationshipBetweenArchivesAndScripts extends Migration {
* @return void
*/
public function down() {
Schema::table('archives', function (Blueprint $table) {
$table->dropForeign(['script_id']);
$table->dropColumn('script_id');
});
Schema::dropIfExists('archive_script');
}
}
<?php
use App\Archive;
use App\Script;
use Illuminate\Database\Seeder;
class ArchivesAndScriptsRelationshipsSeeder extends Seeder {
......@@ -2921,10 +2923,9 @@ class ArchivesAndScriptsRelationshipsSeeder extends Seeder {
*/
public function run() {
foreach ( $this->relationships as $relationship ) {
$archive = App\Archive::where('md5', $relationship['md5'])->first();
$script = App\Script::where('file', $relationship['file'])->first();
$archive->script()->associate($script);
$archive->save();
$archive = Archive::where('md5', $relationship['md5'])->first();
$script_id = Script::where('file', $relationship['file'])->pluck('id')->first();
$archive->scripts()->attach($script_id);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment