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\Validation\ValidatorInterface; |
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 ?ValidatorInterface $validator = null; |
27
|
|
|
|
28
|
|
|
public function __construct(Database $db) |
29
|
|
|
{ |
30
|
|
|
$this->db = $db->getConnection(); |
31
|
|
|
$this->initializeFields(); |
32
|
|
|
$this->initializeMetaDataFields(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function getQuery(): Builder |
36
|
|
|
{ |
37
|
|
|
return $this->db->table($this->table); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function setValidator(ValidatorInterface $validator): void |
41
|
|
|
{ |
42
|
|
|
$this->validator = $validator; |
43
|
|
|
$this->validator->setModel($this); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function getValidator(): ValidatorInterface |
47
|
|
|
{ |
48
|
|
|
return $this->validator; |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function getDeleteType(): string |
52
|
|
|
{ |
53
|
|
|
return $this->deleteType; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function getTableName(): string |
57
|
|
|
{ |
58
|
|
|
return $this->table; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function getPostType(): string |
62
|
|
|
{ |
63
|
|
|
return $this->postType; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|