Skip to content

Commit 75eb583

Browse files
Merge pull request #5 from ssntpl/feature/link_from_parent_data
link child to parent data
2 parents 2729795 + c9c5112 commit 75eb583

8 files changed

Lines changed: 48 additions & 5 deletions

File tree

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ssntpl/data-fields",
3-
"description": "Laravel Permissions.",
3+
"description": "Laravel Data Fields.",
44
"type": "library",
55
"keywords": [
66
"laravel",
@@ -38,6 +38,6 @@
3838
"require": {
3939
"php": "*",
4040
"laravel/framework": "*",
41-
"ssntpl/laravel-files": "^0.1.5"
41+
"ssntpl/laravel-files": "^0.1"
4242
}
4343
}

database/migrations/create_data_fields_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public function up()
1919
$table->string('owner_type');
2020
$table->index(['owner_id', 'owner_type'], 'data_fields_owner_id_owner_type_index');
2121

22+
$table->unsignedBigInteger('parent_id')->nullable();
2223
$table->text('description')->nullable();
2324
$table->string('key')->nullable();
2425
$table->text('value')->nullable();

database/migrations/create_data_sets_table.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ public function up()
1919
$table->string('owner_type');
2020
$table->index(['owner_id', 'owner_type'], 'data_sets_owner_id_owner_type_index');
2121

22+
$table->unsignedBigInteger('parent_id')->nullable();
2223
$table->string('name')->nullable();
2324
$table->string('type');
2425
$table->integer('sort_order')->nullable();

src/Casts/FieldValueCast.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public function set($model, string $key, $value, array $attributes)
3232

3333
return match($attributes['type'] ?? DataField::TEXT) {
3434
DataField::SELECT_MULTIPLE, DataField::ARRAY, DataField::JSON => json_encode($value),
35-
DataField::BOOL => $value ? '1' : '0',
35+
DataField::BOOL => in_array($value, ['1', 'true', 'yes', 'on'], true),
3636
DataField::FILE, DataField::FILES => $this->setFileAsJson($value),
3737
DataField::DATE => Carbon::parse($value)->toDateString(),
3838
DataField::TIME => Carbon::parse($value)->toTimeString(),

src/Models/DataField.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class DataField extends Model
3535
protected $fillable = [
3636
'owner_id',
3737
'owner_type',
38+
'parent_id',
3839
'description',
3940
'key',
4041
'value',
@@ -68,6 +69,11 @@ public static function getAllTypes()
6869
];
6970
}
7071

72+
protected function getClass()
73+
{
74+
return config('data-fields.data_field_model', DataField::class);
75+
}
76+
7177
public function owner()
7278
{
7379
return $this->morphTo();
@@ -84,6 +90,11 @@ public function delete()
8490
{
8591
$dataField->delete();
8692
}
93+
if ($this->children()->get()) {
94+
foreach ($this->children()->get() as $child) {
95+
$child->delete();
96+
}
97+
}
8798
return parent::delete();
8899
}
89100

@@ -100,4 +111,15 @@ public function duplicate()
100111

101112
return $newDataSet;
102113
}
114+
115+
public function parent()
116+
{
117+
return $this->belongsTo($this->getClass(), 'parent_id');
118+
}
119+
120+
public function children()
121+
{
122+
return $this->hasMany($this->getClass(), 'parent_id');
123+
}
124+
103125
}

src/Models/DataSet.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class DataSet extends Model
2020
'id',
2121
'owner_id',
2222
'owner_type',
23+
'parent_id',
2324
'name',
2425
'type',
2526
'sort_order',
@@ -29,6 +30,11 @@ class DataSet extends Model
2930
protected $casts = [
3031
'meta_data' => 'array',
3132
];
33+
34+
protected function getClass()
35+
{
36+
return config('data-fields.data_set_model', DataSet::class);
37+
}
3238

3339
public function owner()
3440
{
@@ -43,6 +49,11 @@ public function delete()
4349
$dataField->delete();
4450
// DataField::destroy($dataField->id);
4551
}
52+
if ($this->children()->get()) {
53+
foreach ($this->children()->get() as $child) {
54+
$child->delete();
55+
}
56+
}
4657
return parent::delete();
4758
}
4859

@@ -60,4 +71,14 @@ public function duplicate()
6071

6172
return $newDataSet;
6273
}
74+
75+
public function parent()
76+
{
77+
return $this->belongsTo($this->getClass(), 'parent_id');
78+
}
79+
80+
public function children()
81+
{
82+
return $this->hasMany($this->getClass(), 'parent_id');
83+
}
6384
}

src/Traits/HasDataFields.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,4 @@ protected function getDataFieldModel()
1515
{
1616
return config('data-fields.data_field_model', DataField::class);
1717
}
18-
1918
}

src/Traits/HasDataSets.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,4 @@ protected function getDataSetModel()
1717
{
1818
return config('data-fields.data_set_model', DataSet::class);
1919
}
20-
2120
}

0 commit comments

Comments
 (0)