Commit 37d98e14 authored by Antoine Le Gonidec's avatar Antoine Le Gonidec

Archive — Improve the way relationships between archives are handled

Related to #7
parent 97d7800e
......@@ -12,30 +12,40 @@ class Archive extends Model {
*
* @var array
*/
protected $appends = [ 'archives', 'archive', 'scripts', 'dependencies' ];
protected $appends = [ 'parents', 'children', 'scripts', 'dependencies' ];
/**
* The attributes that should be hidden for arrays.
*
* @var array
*/
protected $hidden = [ 'archive_id', 'pivot' ];
protected $hidden = [ 'pivot' ];
/**
* Get the archive that requires the current archive
* Get the archives that requires the current archive
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function archive() {
return $this->BelongsTo(Archive::class);
public function parents() {
return $this->BelongsToMany(
Archive::class,
'archives_relationships',
'child_id',
'parent_id'
);
}
public function getArchiveAttribute() {
$archive = $this->archive()->first();
if ( $archive ) {
$archive->makeHidden([ 'size', 'type', 'version', 'archives', 'archive' ]);
}
return $archive;
/**
* Hide some attributes from archives when shown as parents from an archive
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getParentsAttribute() {
$parents = $this->parents()->get();
$parents->each(function ($archive) {
$archive->makeHidden([ 'size', 'type', 'version', 'children' ]);
});
return $parents;
}
/**
......@@ -64,18 +74,29 @@ class Archive extends Model {
/**
* Get the extra archives required by current archive
*
* @return \Illuminate\Database\Eloquent\Relations\HasMany
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function archives() {
return $this->hasMany(Archive::class);
public function children() {
return $this->belongsToMany(
Archive::class,
'archives_relationships',
'parent_id',
'child_id'
)->withPivot('required');
}
public function getArchivesAttribute() {
$collection = $this->archives()->get();
$collection->each(function ($item) {
$item->makeHidden([ 'script', 'size', 'type', 'version', 'archives', 'archive' ]);
/**
* Hide some attributes from archives when shown as children from an archive
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getChildrenAttribute() {
$children = $this->children()->get();
$children->each(function ($archive) {
$archive->makeHidden([ 'script', 'size', 'type', 'version', 'parents' ]);
$archive->required = (bool) $archive->pivot->required;
});
return $collection;
return $children;
}
/**
......
......@@ -11,9 +11,19 @@ class AddRelationshipBetweenArchives extends Migration {
* @return void
*/
public function up() {
Schema::table('archives', function (Blueprint $table) {
$table->integer('archive_id')->unsigned()->nullable();
$table->foreign('archive_id')->references('id')->on('archives');
Schema::create('archives_relationships', function (Blueprint $table) {
$table->unsignedInteger('parent_id');
$table->foreign('parent_id')
->references('id')
->on('archives')
->onDelete('cascade');
$table->unsignedInteger('child_id');
$table->foreign('child_id')
->references('id')
->on('archives')
->onDelete('cascade');
$table->boolean('required')
->default(true);
});
}
......@@ -23,9 +33,6 @@ class AddRelationshipBetweenArchives extends Migration {
* @return void
*/
public function down() {
Schema::table('archives', function (Blueprint $table) {
$table->dropForeign(['archive_id']);
$table->dropColumn('archive_id');
});
Schema::dropIfExists('archives_relationships');
}
}
<?php
use App\Archive;
use Illuminate\Database\Seeder;
class ArchivesRelationshipsSeeder extends Seeder {
......@@ -313,10 +314,14 @@ class ArchivesRelationshipsSeeder extends Seeder {
*/
public function run() {
foreach ( $this->relationships as $relationship ) {
$parent = App\Archive::where('md5', $relationship['parent_md5'])->first();
$child = App\Archive::where('md5', $relationship['child_md5'])->first();
$child->archive()->associate($parent);
$child->save();
$required = isset($relationship['required']) ?
$relationship['required'] : true;
$parent = Archive::where('md5', $relationship['parent_md5'])
->first();
$child_id = Archive::where('md5', $relationship['child_md5'])
->pluck('id')
->first();
$parent->children()->attach($child_id, [ 'required' => $required ]);
}
}
}
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