-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiRepositoryWriteable.php
More file actions
44 lines (39 loc) · 1.54 KB
/
MultiRepositoryWriteable.php
File metadata and controls
44 lines (39 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?php
namespace Squirrel\Entities;
use Squirrel\Debug\Debug;
use Squirrel\Queries\Builder\BuilderInterface;
use Squirrel\Queries\DBException;
/**
* If more than one table needs to be selected or updated at once this class
* combines the knowledge of multiple Repository classes to create
* a query which is simple and secure
*/
class MultiRepositoryWriteable extends MultiRepositoryReadOnly implements MultiRepositoryWriteableInterface
{
public function update(array $repositories, string $query, array $parameters = []): int
{
// Process options and make sure all values are valid
[$sanitizedOptions, $tableName, $objectToTableFields] = $this->processOptions([
'repositories' => [],
'query' => '',
'parameters' => [],
], [
'repositories' => $repositories,
'query' => $query,
'parameters' => $parameters,
], true);
// Process the query
$sqlQuery = $this->buildFreeform($sanitizedOptions['query'], $tableName, $objectToTableFields);
// Execute update query and return number of affected rows from the update
try {
return $this->db->change($sqlQuery, $sanitizedOptions['parameters']);
} catch (DBException $e) {
throw Debug::createException(
\get_class($e),
$e->getMessage(),
ignoreClasses: [MultiRepositoryReadOnlyInterface::class, BuilderInterface::class],
previousException: $e->getPrevious(),
);
}
}
}