|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Jidaikobo\Kontiki\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Connection; |
|
6
|
|
|
use Illuminate\Database\Query\Builder; |
|
7
|
|
|
|
|
8
|
|
|
use Jidaikobo\Kontiki\Core\Database; |
|
9
|
|
|
use Jidaikobo\Kontiki\Models\BaseModelTraits; |
|
10
|
|
|
use Jidaikobo\Kontiki\Services\ValidationService; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* BaseModel provides common CRUD operations for database interactions. |
|
14
|
|
|
* Extend this class to create specific models for different database tables. |
|
15
|
|
|
*/ |
|
16
|
|
|
abstract class BaseModel implements ModelInterface |
|
17
|
|
|
{ |
|
18
|
|
|
use BaseModelTraits\FieldDefinitionTrait; |
|
19
|
|
|
use BaseModelTraits\SearchTrait; |
|
20
|
|
|
use BaseModelTraits\UtilsTrait; |
|
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
protected string $table; |
|
23
|
|
|
protected string $postType = ''; |
|
24
|
|
|
protected string $deleteType = 'hardDelete'; |
|
25
|
|
|
protected Connection $db; |
|
26
|
|
|
public ValidationService $validator; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct( |
|
29
|
|
|
Database $db, |
|
30
|
|
|
ValidationService $validator, |
|
31
|
|
|
) { |
|
32
|
|
|
$this->db = $db->getConnection(); |
|
33
|
|
|
$this->validator = $validator; |
|
34
|
|
|
$this->validator->setModel($this); |
|
35
|
|
|
$this->initializeFields(); |
|
36
|
|
|
$this->initializeMetaDataFields(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
public function getQuery(): Builder |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->db->table($this->table); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function getDeleteType(): string |
|
45
|
|
|
{ |
|
46
|
|
|
return $this->deleteType; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function getTableName(): string |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->table; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
public function getPostType(): string |
|
55
|
|
|
{ |
|
56
|
|
|
return $this->postType; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function validate( |
|
60
|
|
|
array $data, |
|
61
|
|
|
array $context |
|
62
|
|
|
): array { |
|
63
|
|
|
return $this->validator->validate($data, $context); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|