Enabled/Improved links to relationships

This commit is contained in:
Deon George 2016-07-01 14:37:55 +10:00
parent b8be45b42c
commit ad10cb32ce
5 changed files with 57 additions and 23 deletions

1
.gitignore vendored
View File

@ -4,3 +4,4 @@
Homestead.yaml
Homestead.json
.env
.*.swp

View File

@ -66,14 +66,14 @@ class Import extends Command
if ($this->option('tags'))
{
$tags = explode(',',$this->option('tags'));
$t = Tag::whereIn('tag',$tags)->get();
$t = Tag::whereIn('tag',$tags)->pluck('id');
}
// People
if ($this->option('people'))
{
$tags = explode(',',$this->option('people'));
$p = Person::whereIn('tag',$tags)->get();
$p = Person::whereIn('tag',$tags)->pluck('id');
}
$c = 0;
@ -167,25 +167,9 @@ class Import extends Command
if ($po->wasRecentlyCreated)
$this->info(sprintf('Image [%s] stored in DB: %s',$file,$po->id));
// Record our tags
foreach ($t as $o)
if (! (new PhotoTag)->where('tag_id','=',$o->id)->where('photo_id','=',$po->id)->count())
{
$x = new PhotoTag;
$x->tag_id = $o->id;
$x->photo_id = $po->id;
$x->save();
}
// Record our people
foreach ($p as $o)
if (! (new PhotoPerson)->where('people_id','=',$o->id)->where('photo_id','=',$po->id)->count())
{
$x = new PhotoPerson;
$x->people_id = $o->id;
$x->photo_id = $po->id;
$x->save();
}
// Record our people and tags
$po->People()->sync($p->toArray());
$po->Tags()->sync($t->toArray());
}
$bar->finish();

View File

@ -39,10 +39,12 @@ class PhotoDelete extends Job implements ShouldQueue
}
// Remove tags;
// @todo
if ($this->photo->Tags->count())
$this->photo->Tags()->detach();
// Remove People;
// @todo
if ($this->photo->People->count())
$this->photo->People()->detach();
// Make sure our parent is writable
if (! is_writable(dirname($this->photo->file_path())))

View File

@ -19,6 +19,16 @@ class Photo extends Model
8=>-90,
];
public function People()
{
return $this->belongsToMany('App\Model\Person');
}
public function Tags()
{
return $this->belongsToMany('App\Model\Tag');
}
/**
* Photo's NOT pending removal.
*

View File

@ -0,0 +1,37 @@
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class RenamePhotoPeople extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::rename('photo_people', 'person_photo');
Schema::table('person_photo', function (Blueprint $table) {
$table->dropForeign('person_photo_people_id_foreign');
$table->renameColumn('people_id', 'person_id');
$table->foreign('person_id')->references('id')->on('people');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('person_photo', function (Blueprint $table) {
$table->dropForeign('person_photo_person_id_foreign');
$table->renameColumn('person_id','people_id');
$table->foreign('people_id')->references('id')->on('people');
});
Schema::rename('person_photo','photo_people');
}
}