Skip to content

Commit

Permalink
implement mastermodel for BelongsTo relation
Browse files Browse the repository at this point in the history
  • Loading branch information
kolirt committed Sep 6, 2023
1 parent 75f58c9 commit c6b8d9a
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"fill"
],
"license": "MIT",
"version": "2.0.2",
"version": "3.0.2",
"authors": [
{
"name": "kolirt"
Expand Down
49 changes: 37 additions & 12 deletions src/MasterModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
namespace Kolirt\MasterModel;

use Illuminate\Database\Eloquent\MassAssignmentException;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Log;
use Symfony\Component\HttpFoundation\File\UploadedFile;

Expand All @@ -30,19 +32,21 @@ public function delete()
$delete = false;

if ($this->isSoftDeletes()) {
if ($this->trashed())
if ($this->trashed()) {
$delete = true;
}
} else {
$delete = true;
}

if ($delete) {
foreach ($this->attributes as $key => $field) {
if (in_array($key, $this->getFillable())) {
if ($this->isFillable($key)) {
try {
$path = $field;
if (file_exists(storage_path($path)) && !is_dir(storage_path($path)))
if (file_exists(storage_path($path)) && !is_dir(storage_path($path))) {
unlink(storage_path() . $path);
}
} catch (\Exception $e) {
Log::error($e);
}
Expand All @@ -64,7 +68,7 @@ public function delete()
public function fill(array $attributes)
{
foreach ($attributes as $key => $value) {
if (in_array($key, $this->getFillable())) {
if ($this->isFillable($key)) {
if ($value instanceof UploadedFile) {
if ($value->isValid()) {
$dir_path = '/app/public/uploads/' . mb_strtolower(class_basename($this)) . '/';
Expand Down Expand Up @@ -113,20 +117,18 @@ public function fill(array $attributes)
$this->relationsToSave[$key] = [$relation, $value];
} else if ($relation instanceof BelongsTo) {
$foreignKeyName = $relation->getForeignKeyName();
if (is_object($value)) {
if (method_exists($value, 'getKey')) {
$newAttributes[$foreignKeyName] = $value->getKey();
}
} else {
if ($value instanceof Model) {
$newAttributes[$foreignKeyName] = $value->getKey();
} else if (is_int($value)) {
$newAttributes[$foreignKeyName] = $value;
} else if (is_array($value) || $value instanceof Collection) {
$this->priorityRelationsToSave[$key] = [$relation, $value];
}
}
}
}

$model = parent::fill($newAttributes);

return $model;
return parent::fill($newAttributes);
}

/**
Expand All @@ -141,6 +143,29 @@ public function save(array $options = [])
try {
\DB::beginTransaction();

$priorityRelationsToSave = $this->priorityRelationsToSave;
$this->priorityRelationsToSave = [];
foreach ($priorityRelationsToSave as $key => $rel) {
$relation = $rel[0];
$data = $rel[1];

if ($relation instanceof BelongsTo) {
$ownerKeyName = $relation->getOwnerKeyName();
if (isset($data[$ownerKeyName]) && $data[$ownerKeyName]) {
$related = $relation->getRelated()->where($ownerKeyName, $data[$ownerKeyName])->first();
if ($related) {
unset($data[$ownerKeyName]);
$related->update($data);
}
} else {
$item = $relation->create($data);
$this->fill([
$relation->getForeignKeyName() => $item->getKey()
]);
}
}
}

$saved = parent::save($options);

$relationsToSave = $this->relationsToSave;
Expand Down

0 comments on commit c6b8d9a

Please sign in to comment.