1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the eav package. |
4
|
|
|
* @author Aleksandr Drobotik <[email protected]> |
5
|
|
|
* @copyright 2023 Aleksandr Drobotik |
6
|
|
|
* @license https://opensource.org/license/mit The MIT License |
7
|
|
|
*/ |
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Drobotik\Eav\Model; |
11
|
|
|
|
12
|
|
|
use Drobotik\Eav\Database\Connection; |
13
|
|
|
use Drobotik\Eav\Enum\_VALUE; |
14
|
|
|
use Drobotik\Eav\Enum\ATTR_TYPE; |
15
|
|
|
use Drobotik\Eav\Import\Content\ValueSet; |
16
|
|
|
use Drobotik\Eav\Trait\SingletonsTrait; |
17
|
|
|
use InvalidArgumentException; |
18
|
|
|
|
19
|
|
|
class ValueBase extends Model |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
use SingletonsTrait; |
23
|
1 |
|
public function __construct() |
24
|
|
|
{ |
25
|
1 |
|
$this->setPrimaryKey(_VALUE::ID); |
26
|
|
|
} |
27
|
|
|
|
28
|
1 |
|
public function find(string $table, int $domainKey, int $entityKey, int $attributeKey) : bool|array |
29
|
|
|
{ |
30
|
1 |
|
return $this->db() |
31
|
1 |
|
->createQueryBuilder() |
32
|
1 |
|
->select('*') |
33
|
1 |
|
->from($table) |
34
|
1 |
|
->where(sprintf('%s = :domain AND %s = :entity AND %s = :attr', |
35
|
1 |
|
_VALUE::DOMAIN_ID, _VALUE::ENTITY_ID, _VALUE::ATTRIBUTE_ID |
36
|
1 |
|
)) |
37
|
1 |
|
->setParameters([ |
38
|
1 |
|
"domain" => $domainKey, |
39
|
1 |
|
"entity" => $entityKey, |
40
|
1 |
|
"attr" => $attributeKey |
41
|
1 |
|
]) |
42
|
1 |
|
->executeQuery() |
43
|
1 |
|
->fetchAssociative(); |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
public function create(string $table, int $domainKey, int $entityKey, int $attributeKey, $value) : int |
47
|
|
|
{ |
48
|
1 |
|
$conn = $this->db(); |
49
|
|
|
|
50
|
1 |
|
$conn->createQueryBuilder() |
51
|
1 |
|
->insert($table) |
52
|
1 |
|
->values([ |
53
|
1 |
|
_VALUE::DOMAIN_ID => '?', |
54
|
1 |
|
_VALUE::ENTITY_ID => '?', |
55
|
1 |
|
_VALUE::ATTRIBUTE_ID => '?', |
56
|
1 |
|
_VALUE::VALUE => '?', |
57
|
1 |
|
]) |
58
|
1 |
|
->setParameter(0, $domainKey) |
59
|
1 |
|
->setParameter(1, $entityKey) |
60
|
1 |
|
->setParameter(2, $attributeKey) |
61
|
1 |
|
->setParameter(3, $value) |
62
|
1 |
|
->executeQuery(); |
63
|
1 |
|
return (int) $conn->lastInsertId(); |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
public function update(string $table, int $domainKey, int $entityKey, int $attributeKey, $value) : int |
67
|
|
|
{ |
68
|
1 |
|
$conn = $this->db(); |
69
|
1 |
|
return $conn->createQueryBuilder() |
70
|
1 |
|
->update($table) |
71
|
1 |
|
->where(sprintf('%s = :domain AND %s = :entity AND %s = :attr', |
72
|
1 |
|
_VALUE::DOMAIN_ID, _VALUE::ENTITY_ID, _VALUE::ATTRIBUTE_ID |
73
|
1 |
|
)) |
74
|
1 |
|
->set(_VALUE::VALUE, ':value') |
75
|
1 |
|
->setParameters([ |
76
|
1 |
|
"domain" => $domainKey, |
77
|
1 |
|
"entity" => $entityKey, |
78
|
1 |
|
"attr" => $attributeKey, |
79
|
1 |
|
"value" => $value |
80
|
1 |
|
]) |
81
|
1 |
|
->executeQuery() |
82
|
1 |
|
->rowCount(); |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
public function destroy(string $table, int $domainKey, int $entityKey, int $attributeKey) : int |
86
|
|
|
{ |
87
|
1 |
|
return $this->db() |
88
|
1 |
|
->createQueryBuilder() |
89
|
1 |
|
->delete($table) |
90
|
1 |
|
->where(sprintf('%s = ? AND %s = ? AND %s = ?', |
91
|
1 |
|
_VALUE::DOMAIN_ID, _VALUE::ENTITY_ID, _VALUE::ATTRIBUTE_ID |
92
|
1 |
|
)) |
93
|
1 |
|
->setParameters([$domainKey, $entityKey, $attributeKey]) |
94
|
1 |
|
->executeQuery() |
95
|
1 |
|
->rowCount(); |
96
|
|
|
} |
97
|
|
|
|
98
|
1 |
|
public function bulkCreate(ValueSet $valueSet, int $domainKey): void |
99
|
|
|
{ |
100
|
1 |
|
$pdo = Connection::get()->getNativeConnection(); |
101
|
|
|
|
102
|
1 |
|
$template = "INSERT INTO %s ("._VALUE::DOMAIN_ID.","._VALUE::ENTITY_ID.","._VALUE::ATTRIBUTE_ID.","._VALUE::VALUE.")"; |
103
|
|
|
|
104
|
1 |
|
$stringTable = ATTR_TYPE::valueTable(ATTR_TYPE::STRING); |
105
|
1 |
|
$integerTable = ATTR_TYPE::valueTable(ATTR_TYPE::INTEGER); |
106
|
1 |
|
$decimalTable = ATTR_TYPE::valueTable(ATTR_TYPE::DECIMAL); |
107
|
1 |
|
$datetimeTable = ATTR_TYPE::valueTable(ATTR_TYPE::DATETIME); |
108
|
1 |
|
$textTable = ATTR_TYPE::valueTable(ATTR_TYPE::TEXT); |
109
|
|
|
|
110
|
1 |
|
$stringTemplate = sprintf($template, $stringTable) . " VALUES %s;"; |
111
|
1 |
|
$integerTemplate = sprintf($template, $integerTable) . " VALUES %s;"; |
112
|
1 |
|
$decimalTemplate = sprintf($template, $decimalTable) . " VALUES %s;"; |
113
|
1 |
|
$datetimeTemplate = sprintf($template, $datetimeTable) . " VALUES %s;"; |
114
|
1 |
|
$textTemplate = sprintf($template, $textTable) . " VALUES %s;"; |
115
|
|
|
|
116
|
1 |
|
$stringBulk = []; |
117
|
1 |
|
$integerBulk = []; |
118
|
1 |
|
$decimalBulk = []; |
119
|
1 |
|
$datetimeBulk = []; |
120
|
1 |
|
$textBulk = []; |
121
|
|
|
|
122
|
1 |
|
foreach ($valueSet->forExistingEntities() as $data) { |
123
|
1 |
|
$attributeKey = $data->getAttributeKey(); |
124
|
1 |
|
$value = $data->getValue(); |
125
|
1 |
|
$table = ATTR_TYPE::valueTable($data->getType()); |
126
|
1 |
|
$entityKey = $data->getEntityKey(); |
127
|
1 |
|
$bulkTemplate = "($domainKey, $entityKey, $attributeKey, '$value')"; |
128
|
|
|
switch ($table) { |
129
|
1 |
|
case $stringTable: |
130
|
1 |
|
$stringBulk[] = $bulkTemplate; |
131
|
1 |
|
break; |
132
|
|
|
|
133
|
1 |
|
case $integerTable: |
134
|
1 |
|
$integerBulk[] = $bulkTemplate; |
135
|
1 |
|
break; |
136
|
|
|
|
137
|
1 |
|
case $decimalTable: |
138
|
1 |
|
$decimalBulk[] = $bulkTemplate; |
139
|
1 |
|
break; |
140
|
|
|
|
141
|
1 |
|
case $datetimeTable: |
142
|
1 |
|
$datetimeBulk[] = $bulkTemplate; |
143
|
1 |
|
break; |
144
|
|
|
|
145
|
1 |
|
case $textTable: |
146
|
1 |
|
$textBulk[] = $bulkTemplate; |
147
|
1 |
|
break; |
148
|
|
|
|
149
|
|
|
default: |
150
|
|
|
throw new InvalidArgumentException("Unhandled table: " . $table); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
154
|
1 |
|
if(count($stringBulk) > 0) { |
155
|
1 |
|
$pdo->exec(sprintf($stringTemplate, implode(',', $stringBulk))); |
156
|
|
|
} |
157
|
1 |
|
if(count($integerBulk) > 0) { |
158
|
1 |
|
$pdo->exec(sprintf($integerTemplate, implode(',', $integerBulk))); |
159
|
|
|
} |
160
|
1 |
|
if(count($decimalBulk) > 0) { |
161
|
1 |
|
$pdo->exec(sprintf($decimalTemplate, implode(',', $decimalBulk))); |
162
|
|
|
} |
163
|
1 |
|
if(count($datetimeBulk) > 0) { |
164
|
1 |
|
$pdo->exec(sprintf($datetimeTemplate, implode(',', $datetimeBulk))); |
165
|
|
|
} |
166
|
1 |
|
if(count($textBulk) > 0) { |
167
|
1 |
|
$pdo->exec(sprintf($textTemplate, implode(',', $textBulk))); |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
} |