diff --git a/.gitignore b/.gitignore index 6b3af3f..d001d72 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ Homestead.yaml Homestead.json .env +.*.swp diff --git a/app/Console/Commands/Import.php b/app/Console/Commands/Import.php index adfbcff..1532a72 100644 --- a/app/Console/Commands/Import.php +++ b/app/Console/Commands/Import.php @@ -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(); diff --git a/app/Jobs/PhotoDelete.php b/app/Jobs/PhotoDelete.php index cd986bb..26574d6 100644 --- a/app/Jobs/PhotoDelete.php +++ b/app/Jobs/PhotoDelete.php @@ -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()))) diff --git a/app/Model/Photo.php b/app/Model/Photo.php index 6590d24..de0570d 100644 --- a/app/Model/Photo.php +++ b/app/Model/Photo.php @@ -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. * diff --git a/database/migrations/2016_07_01_131722_rename_photo_people.php b/database/migrations/2016_07_01_131722_rename_photo_people.php new file mode 100644 index 0000000..ad7f18c --- /dev/null +++ b/database/migrations/2016_07_01_131722_rename_photo_people.php @@ -0,0 +1,37 @@ +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'); + } +}