Skip to content

Commit aad8c31

Browse files
committed
fix: support $guarded models and MariaDB driver
- Replace mergeFillable with isFillable() and fillableFromArray() overrides so models using $guarded instead of $fillable can save custom fields - Add MariaDB to text_value driver-specific constraints (matches MySQL) Closes #121 Closes #120
1 parent e3c1f23 commit aad8c31

File tree

4 files changed

+113
-1
lines changed

4 files changed

+113
-1
lines changed

src/Models/Concerns/UsesCustomFields.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,33 @@ public function __construct($attributes = [])
3131

3232
parent::__construct($attributes);
3333

34-
// Handle custom_fields immediately if present in attributes
3534
$this->handleCustomFields();
3635
}
3736

37+
public function isFillable($key): bool
38+
{
39+
if ($key === 'custom_fields') {
40+
return true;
41+
}
42+
43+
return parent::isFillable($key);
44+
}
45+
46+
/**
47+
* @param array<string, mixed> $attributes
48+
* @return array<string, mixed>
49+
*/
50+
protected function fillableFromArray(array $attributes): array
51+
{
52+
$fillable = parent::fillableFromArray($attributes);
53+
54+
if (array_key_exists('custom_fields', $attributes) && ! array_key_exists('custom_fields', $fillable)) {
55+
$fillable['custom_fields'] = $attributes['custom_fields'];
56+
}
57+
58+
return $fillable;
59+
}
60+
3861
/**
3962
* @var array<int, array<string, mixed>>
4063
*/

src/Support/DatabaseFieldConstraints.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ final class DatabaseFieldConstraints
3838
'text_value' => [
3939
'max' => [
4040
'mysql' => 65535,
41+
'mariadb' => 65535,
4142
'pgsql' => 1073741823,
4243
'sqlite' => 1000000000,
4344
],
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
use Relaticle\CustomFields\Models\Concerns\UsesCustomFields;
7+
use Relaticle\CustomFields\Models\Contracts\HasCustomFields;
8+
use Relaticle\CustomFields\Tests\Fixtures\Models\User;
9+
10+
beforeEach(function (): void {
11+
$this->user = User::factory()->create();
12+
$this->actingAs($this->user);
13+
});
14+
15+
describe('Fillable Model Support', function (): void {
16+
it('includes custom_fields in getFillable for models with $fillable', function (): void {
17+
$model = new FillableTestModel;
18+
19+
expect($model->getFillable())->toContain('custom_fields');
20+
});
21+
22+
it('recognizes custom_fields as fillable via isFillable on fillable models', function (): void {
23+
$model = new FillableTestModel;
24+
25+
expect($model->isFillable('custom_fields'))->toBeTrue();
26+
});
27+
});
28+
29+
describe('Guarded Model Support', function (): void {
30+
it('recognizes custom_fields as fillable via isFillable override', function (): void {
31+
$model = new GuardedTestModel;
32+
33+
expect($model->isFillable('custom_fields'))->toBeTrue()
34+
->and($model->getGuarded())->toBe(['id']);
35+
});
36+
37+
it('allows mass assignment of custom_fields on guarded models', function (): void {
38+
$model = new GuardedTestModel;
39+
$model->fill([
40+
'title' => 'Test Title',
41+
'custom_fields' => ['field_one' => 'value_one'],
42+
]);
43+
44+
expect($model->title)->toBe('Test Title');
45+
});
46+
47+
it('stores custom_fields in temp storage when filling a guarded model', function (): void {
48+
$model = new GuardedTestModel;
49+
$model->fill(['custom_fields' => ['test_field' => 'test_value']]);
50+
51+
expect($model->custom_fields)->toBe(['test_field' => 'test_value']);
52+
});
53+
});
54+
55+
class FillableTestModel extends Model implements HasCustomFields
56+
{
57+
use UsesCustomFields;
58+
59+
protected $table = 'posts';
60+
61+
protected $fillable = ['title'];
62+
}
63+
64+
class GuardedTestModel extends Model implements HasCustomFields
65+
{
66+
use UsesCustomFields;
67+
68+
protected $table = 'posts';
69+
70+
protected $guarded = ['id'];
71+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use Relaticle\CustomFields\Support\DatabaseFieldConstraints;
6+
7+
it('includes mariadb in text_value driver-specific constraints', function (): void {
8+
$constraints = DatabaseFieldConstraints::getConstraintsForColumn('text_value');
9+
10+
expect($constraints['max'])->toHaveKey('mariadb');
11+
});
12+
13+
it('has the same max value for mariadb as mysql', function (): void {
14+
$constraints = DatabaseFieldConstraints::getConstraintsForColumn('text_value');
15+
16+
expect($constraints['max']['mariadb'])->toBe($constraints['max']['mysql']);
17+
});

0 commit comments

Comments
 (0)