1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Quantum PHP Framework |
5
|
|
|
* |
6
|
|
|
* An open source software development framework for PHP |
7
|
|
|
* |
8
|
|
|
* @package Quantum |
9
|
|
|
* @author Arman Ag. <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2018 Softberg LLC (https://softberg.org) |
11
|
|
|
* @link http://quantum.softberg.org/ |
12
|
|
|
* @since 2.6.0 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Quantum\Libraries\Database\Sleekdb\Statements; |
16
|
|
|
|
17
|
|
|
use Quantum\Libraries\Database\DbalInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Trait Model |
21
|
|
|
* @package Quantum\Libraries\Database\Sleekdb\Statements |
22
|
|
|
*/ |
23
|
|
|
trait Model |
24
|
|
|
{ |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @inheritDoc |
28
|
|
|
*/ |
29
|
|
|
public function create(): DbalInterface |
30
|
|
|
{ |
31
|
|
|
$this->isNew = true; |
|
|
|
|
32
|
|
|
return $this; |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @inheritDoc |
37
|
|
|
*/ |
38
|
|
|
public function prop(string $key, $value = null) |
39
|
|
|
{ |
40
|
|
|
if ($value) { |
41
|
|
|
$this->modifiedFields[$key] = $value; |
|
|
|
|
42
|
|
|
} else { |
43
|
|
|
return $this->modifiedFields[$key] ?? null; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritDoc |
49
|
|
|
* @throws \SleekDB\Exceptions\IOException |
50
|
|
|
* @throws \SleekDB\Exceptions\InvalidConfigurationException |
51
|
|
|
* @throws \Quantum\Exceptions\DatabaseException |
52
|
|
|
* @throws \SleekDB\Exceptions\InvalidArgumentException |
53
|
|
|
*/ |
54
|
|
|
public function save(): bool |
55
|
|
|
{ |
56
|
|
|
$ormMode = $this->getOrmModel(); |
|
|
|
|
57
|
|
|
|
58
|
|
|
if ($this->isNew) { |
59
|
|
|
$ormMode->insert($this->modifiedFields); |
60
|
|
|
} else { |
61
|
|
|
$ormMode->update($this->modifiedFields); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return true; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @inheritDoc |
69
|
|
|
* @throws \Quantum\Exceptions\DatabaseException |
70
|
|
|
* @throws \SleekDB\Exceptions\IOException |
71
|
|
|
* @throws \SleekDB\Exceptions\InvalidArgumentException |
72
|
|
|
* @throws \SleekDB\Exceptions\InvalidConfigurationException |
73
|
|
|
*/ |
74
|
|
|
public function delete(): bool |
75
|
|
|
{ |
76
|
|
|
$pk = $this->getOrmModel()->getPrimaryKey(); |
77
|
|
|
return $this->getOrmModel()->deleteById($this->data[$pk]); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @inheritDoc |
82
|
|
|
* @throws \Quantum\Exceptions\DatabaseException |
83
|
|
|
* @throws \SleekDB\Exceptions\IOException |
84
|
|
|
* @throws \SleekDB\Exceptions\InvalidArgumentException |
85
|
|
|
* @throws \SleekDB\Exceptions\InvalidConfigurationException |
86
|
|
|
*/ |
87
|
|
|
public function deleteMany(): bool |
88
|
|
|
{ |
89
|
|
|
return $this->getBuilder()->getQuery()->delete(); |
|
|
|
|
90
|
|
|
} |
91
|
|
|
} |