Skip to content

Commit 0f636a7

Browse files
author
Marius
committed
Adding model attributes autocomplete to model
1 parent dc7deed commit 0f636a7

File tree

3 files changed

+92
-2
lines changed

3 files changed

+92
-2
lines changed

README.md

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,25 @@ Create a constant in your code
4747
];
4848
}
4949

50-
Extend BaseModel (it needs datetime NOT timestamp for created_at and updated_at columns) and BaseResourceService and use ResourceControllerTrait in a BaseController (that you extend) and add this new resource to the above map.
50+
Extend:
5151

52-
The BaseController should call $this->init() from its construct.
52+
- BaseModel (it needs datetime NOT timestamp for created_at and updated_at columns)
53+
- BaseResourceService
54+
55+
Create a Controller that uses ResourceControllerTrait and call $this->init() from its __construct.
56+
57+
For model properties autocomplete:
58+
59+
- extend BaseModelAttributes following the same FQN structure as the parent's:
60+
61+
\App\Models\ChildBaseModel paired with \App\Models\Attributes\ChildBaseModelAttributes
62+
\App\Models\Folder\ChildBaseModel paired with \App\Models\Folder\Attributes\ChildBaseModelAttributes
63+
64+
- add in its class dock block using **@property** all the models properties/attributes/columns
65+
- add in the model's class dock block **@property ChildBaseModelAttributes $a**
66+
- use **$model->a->** instead of **$model->**
67+
68+
Add this new resource to the above map.
5369

5470
Register the crud routes in your application using (for example in Laravel)
5571

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
namespace MacropaySolutions\LaravelCrudWizard\Models\Attributes;
4+
5+
use Illuminate\Support\Traits\ForwardsCalls;
6+
use Transactions\Crud\Models\BaseModel;
7+
8+
/**
9+
* For properties autocompletion declare in the children classes (with @ property) all the model's parameters (columns)
10+
*/
11+
class BaseModelAttributes
12+
{
13+
use ForwardsCalls;
14+
15+
private const PRIVATE_ATTRIBUTE = 'ownerBaseModel';
16+
17+
public function __construct(
18+
private BaseModel $ownerBaseModel
19+
) {
20+
}
21+
22+
public function __get(string $key): mixed
23+
{
24+
if ($key === self::PRIVATE_ATTRIBUTE) {
25+
return null;
26+
}
27+
28+
return $this->ownerBaseModel->getAttribute($key);
29+
}
30+
31+
public function __set(string $key, mixed $value): void
32+
{
33+
if ($key === self::PRIVATE_ATTRIBUTE) {
34+
return;
35+
}
36+
37+
$this->ownerBaseModel->setAttribute($key, $value);
38+
}
39+
40+
public function __isset(string $key): bool
41+
{
42+
if ($key === self::PRIVATE_ATTRIBUTE) {
43+
return true;
44+
}
45+
46+
return $this->ownerBaseModel->__isset($key);
47+
}
48+
49+
public function __unset(string $key): void
50+
{
51+
if ($key === self::PRIVATE_ATTRIBUTE) {
52+
return;
53+
}
54+
55+
$this->ownerBaseModel->__unset($key);
56+
}
57+
58+
public function __call(string $method, array $parameters): mixed
59+
{
60+
return $this->forwardCallTo($this->ownerBaseModel, $method, $parameters);
61+
}
62+
}

src/Models/BaseModel.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
use Illuminate\Support\Facades\DB;
1010
use Illuminate\Support\Facades\Log;
1111
use MacropaySolutions\LaravelCrudWizard\Helpers\GeneralHelper;
12+
use MacropaySolutions\LaravelCrudWizard\Models\Attributes\BaseModelAttributes;
1213

14+
/**
15+
* For properties autocompletion declare in the children classes (with @ property) ChildBaseModelAttributes $a
16+
*/
1317
abstract class BaseModel extends Model
1418
{
1519
public const RESOURCE_NAME = null;
@@ -18,6 +22,7 @@ abstract class BaseModel extends Model
1822
public const UPDATED_AT_FORMAT = 'Y-m-d H:i:s';
1923
public const COMPOSITE_PK_SEPARATOR = '_';
2024
public $timestamps = false;
25+
public BaseModelAttributes $a;
2126
protected array $ignoreUpdateFor = [];
2227
protected array $ignoreExternalCreateFor = [];
2328
protected array $allowNonExternalUpdatesFor = [];
@@ -33,6 +38,13 @@ public function __construct(array $attributes = [])
3338
{
3439
parent::__construct($attributes);
3540

41+
$attributes = \substr($class = static::class, 0, $l = (-1 * (\strlen($class) - \strrpos($class, '\\') - 1))) .
42+
'Attributes\\' . \substr($class, $l) . 'Attributes';
43+
44+
if (\class_exists($attributes)) {
45+
$this->a = new $attributes($this);
46+
}
47+
3648
$this->append('primary_key_identifier');
3749
}
3850

0 commit comments

Comments
 (0)