-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInsert.php
57 lines (45 loc) · 1.1 KB
/
Insert.php
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
<?php
namespace ModernPDO\Actions;
use ModernPDO\Traits\ColumnsTrait;
use ModernPDO\Traits\ValuesTrait;
/**
* Class for inserting rows to a table.
*/
class Insert extends Action
{
use ColumnsTrait;
use ValuesTrait;
/**
* Returns base query.
*/
protected function buildQuery(): string
{
$escaper = $this->mpdo->escaper();
$query = 'INSERT INTO ' . $escaper->table($this->table);
if (!empty($this->columns)) {
$query .= ' (' . $this->columnsQuery($escaper) . ')';
}
$query .= ' VALUES ' . $this->valuesQuery($escaper);
return trim($query);
}
/**
* Returns placeholders.
*
* @return mixed[]
*/
protected function getPlaceholders(): array
{
return array_merge($this->columnsPlaceholders(), $this->valuesPlaceholders());
}
/**
* Inserts row into table.
*/
public function execute(): bool
{
if (empty($this->values)) {
return false;
}
$this->query = $this->buildQuery();
return $this->exec()->rowCount() > 0;
}
}