-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySQLRepositoryAbstract.php
More file actions
67 lines (54 loc) · 1.55 KB
/
MySQLRepositoryAbstract.php
File metadata and controls
67 lines (54 loc) · 1.55 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
declare(strict_types=1);
namespace VC4A\Repository;
use PDO;
use PDOException;
use VC4A\Model\DocumentsModel;
abstract class MySQLRepositoryAbstract implements MySQLRepositoryInterface
{
/** @var PDO */
private $pdo;
/**
* @param PDO $pdo
*/
public function __construct(PDO $pdo)
{
$this->pdo = $pdo;
}
/**
* @throws PDOException
* @return array
*/
public function all(): array
{
$sql = "SELECT "
. implode(', ', $this->getAllColumnNames())
. " FROM " . $this->getTableName();
$statement = $this->pdo->prepare($sql);
$statement->execute();
return $statement->fetchAll(PDO::FETCH_ASSOC);
}
/**
* @throws PDOException
* @param array $data
* @return array
*/
public function create(array $data): array
{
$tableName = $this->getTableName();
$columnNames = $this->getCreateColumnNames();
$rowPlaces = '(' . implode(', ', array_fill(0, count($columnNames), '?')) . ')';
$allPlaces = implode(', ', array_fill(0, count($data), $rowPlaces));
$sql = "INSERT INTO $tableName (" . implode(', ', $columnNames) .
") VALUES " . $allPlaces;
$this->pdo->beginTransaction();
$statement = $this->pdo->prepare($sql);
$dataToInsert = [];
foreach ($data as $file) {
$dataToInsert[] = $file[DocumentsModel::FILENAME];
}
$statement->execute($dataToInsert);
$this->pdo->commit();
return $data;
}
}