Skip to content

Commit 6175bbb

Browse files
committed
Fix factory
1 parent 77f3814 commit 6175bbb

File tree

4 files changed

+22
-15
lines changed

4 files changed

+22
-15
lines changed

database/Factories/AssetFactory.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace CraftCms\Cms\Database\Factories;
66

77
use CraftCms\Cms\Asset\Models\Asset;
8+
use CraftCms\Cms\Asset\Models\Volume;
89
use CraftCms\Cms\Asset\Models\VolumeFolder;
910
use CraftCms\Cms\Element\Models\Element;
1011
use Illuminate\Database\Eloquent\Factories\Factory;
@@ -19,6 +20,7 @@ public function definition(): array
1920
{
2021
return [
2122
'id' => Element::factory()->set('type', \CraftCms\Cms\Element\Elements\Asset::class),
23+
'volumeId' => Volume::factory(),
2224
'folderId' => VolumeFolder::factory(),
2325
'filename' => fake()->word().'.jpg',
2426
'kind' => 'image',
@@ -29,12 +31,16 @@ public function definition(): array
2931
public function configure(): self
3032
{
3133
return $this->afterCreating(function (Asset $asset) {
32-
$asset->element->update([
33-
'dateCreated' => $asset->dateCreated,
34-
'dateUpdated' => $asset->dateCreated,
35-
]);
36-
37-
$asset->update(['volumeId' => $asset->folder->volume?->id]);
34+
// For some reason the element factory doesn't get saved properly
35+
if ($asset->id === 0) {
36+
$asset->update([
37+
'id' => Element::query()
38+
->where('type', \CraftCms\Cms\Element\Elements\Asset::class)
39+
->latest('id')
40+
->first()
41+
->id,
42+
]);
43+
}
3844
});
3945
}
4046
}

src/Asset/Models/Asset.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Illuminate\Database\Eloquent\Factories\HasFactory;
1313
use Illuminate\Database\Eloquent\Relations\BelongsTo;
1414
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
15+
use Illuminate\Database\Eloquent\Relations\Pivot;
1516

1617
final class Asset extends BaseModel
1718
{
@@ -32,15 +33,15 @@ protected function casts(): array
3233
}
3334

3435
/**
35-
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\CraftCms\Cms\Element\Models\Element, $this>
36+
* @return BelongsTo<Element, $this>
3637
*/
3738
public function element(): BelongsTo
3839
{
3940
return $this->belongsTo(Element::class, 'id');
4041
}
4142

4243
/**
43-
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany<\CraftCms\Cms\Site\Models\Site, $this, \Illuminate\Database\Eloquent\Relations\Pivot>
44+
* @return BelongsToMany<Site, $this, Pivot>
4445
*/
4546
public function sites(): BelongsToMany
4647
{
@@ -49,23 +50,23 @@ public function sites(): BelongsToMany
4950
}
5051

5152
/**
52-
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\CraftCms\Cms\Asset\Models\Volume, $this>
53+
* @return BelongsTo<Volume, $this>
5354
*/
5455
public function volume(): BelongsTo
5556
{
5657
return $this->belongsTo(Volume::class, 'volumeId');
5758
}
5859

5960
/**
60-
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\CraftCms\Cms\Asset\Models\VolumeFolder, $this>
61+
* @return BelongsTo<VolumeFolder, $this>
6162
*/
6263
public function folder(): BelongsTo
6364
{
6465
return $this->belongsTo(VolumeFolder::class, 'folderId');
6566
}
6667

6768
/**
68-
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\CraftCms\Cms\User\Models\User, $this>
69+
* @return BelongsTo<User, $this>
6970
*/
7071
public function uploader(): BelongsTo
7172
{

src/Asset/Models/Volume.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ protected function casts(): array
2828
}
2929

3030
/**
31-
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\CraftCms\Cms\FieldLayout\Models\FieldLayout, $this>
31+
* @return BelongsTo<FieldLayout, $this>
3232
*/
3333
public function fieldLayout(): BelongsTo
3434
{

src/Asset/Models/VolumeFolder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,23 @@ final class VolumeFolder extends BaseModel
1919
protected $table = Table::VOLUMEFOLDERS;
2020

2121
/**
22-
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\CraftCms\Cms\Asset\Models\VolumeFolder, $this>
22+
* @return BelongsTo<VolumeFolder, $this>
2323
*/
2424
public function parent(): BelongsTo
2525
{
2626
return $this->belongsTo(self::class, 'parentId');
2727
}
2828

2929
/**
30-
* @return \Illuminate\Database\Eloquent\Relations\HasMany<\CraftCms\Cms\Asset\Models\VolumeFolder, $this>
30+
* @return HasMany<VolumeFolder, $this>
3131
*/
3232
public function children(): HasMany
3333
{
3434
return $this->hasMany(self::class, 'parentId');
3535
}
3636

3737
/**
38-
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo<\CraftCms\Cms\Asset\Models\Volume, $this>
38+
* @return BelongsTo<Volume, $this>
3939
*/
4040
public function volume(): BelongsTo
4141
{

0 commit comments

Comments
 (0)