1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace LaravelFreelancerNL\Aranguent\Eloquent; |
6
|
|
|
|
7
|
|
|
use Illuminate\Database\Eloquent\Builder as IlluminateEloquentBuilder; |
8
|
|
|
use Illuminate\Support\Arr; |
9
|
|
|
use LaravelFreelancerNL\Aranguent\Eloquent\Concerns\QueriesAranguentRelationships; |
10
|
|
|
use LaravelFreelancerNL\Aranguent\Query\Builder as QueryBuilder; |
11
|
|
|
|
12
|
|
|
class Builder extends IlluminateEloquentBuilder |
13
|
|
|
{ |
14
|
|
|
use QueriesAranguentRelationships; |
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The base query builder instance. |
18
|
|
|
* |
19
|
|
|
* @var QueryBuilder |
20
|
|
|
*/ |
21
|
|
|
protected $query; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Insert a record in the database. |
25
|
|
|
* |
26
|
|
|
* |
27
|
|
|
* @param array<mixed> $values |
28
|
|
|
* @return bool |
29
|
|
|
*/ |
30
|
4 |
|
public function insert(array $values) |
31
|
|
|
{ |
32
|
4 |
|
// Since every insert gets treated like a batch insert, we will make sure the |
33
|
4 |
|
// bindings are structured in a way that is convenient when building these |
34
|
2 |
|
// inserts statements by verifying these elements are actually an array. |
35
|
|
|
if (empty($values)) { |
36
|
|
|
return true; |
37
|
2 |
|
} |
38
|
2 |
|
|
39
|
2 |
|
if (Arr::isAssoc($values)) { |
40
|
|
|
$values = [$values]; |
41
|
|
|
} |
42
|
|
|
if (!Arr::isAssoc($values)) { |
43
|
|
|
// Here, we will sort the insert keys for every record so that each insert is |
44
|
|
|
// in the same order for the record. We need to make sure this is the case |
45
|
|
|
// so there are not any errors or problems when inserting these records. |
46
|
|
|
foreach ($values as $key => $value) { |
47
|
|
|
ksort($value); |
48
|
|
|
|
49
|
3 |
|
$values[$key] = $value; |
50
|
|
|
} |
51
|
3 |
|
} |
52
|
3 |
|
|
53
|
2 |
|
//Set timestamps |
54
|
|
|
foreach ($values as $key => $value) { |
55
|
|
|
$values[$key] = $this->updateTimestamps($value); |
56
|
1 |
|
} |
57
|
|
|
|
58
|
|
|
return $this->toBase()->insert($values); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Add the "updated at" column to an array of values. |
63
|
|
|
* |
64
|
|
|
* @param array<string, string> $values |
65
|
|
|
* @return array<string, string> |
66
|
39 |
|
*/ |
67
|
|
|
protected function updateTimestamps(array $values) |
68
|
|
|
{ |
69
|
|
|
if ( |
70
|
|
|
!$this->model->usesTimestamps() || |
71
|
39 |
|
is_null($this->model->getUpdatedAtColumn()) || |
72
|
|
|
is_null($this->model->getCreatedAtColumn()) |
73
|
|
|
) { |
74
|
|
|
return $values; |
75
|
39 |
|
} |
76
|
11 |
|
|
77
|
|
|
$timestamp = $this->model->freshTimestampString(); |
78
|
39 |
|
$updatedAtColumn = $this->model->getUpdatedAtColumn(); |
79
|
|
|
|
80
|
|
|
$timestamps = []; |
81
|
|
|
$timestamps[$updatedAtColumn] = $timestamp; |
82
|
39 |
|
|
83
|
39 |
|
$createdAtColumn = $this->model->getCreatedAtColumn(); |
84
|
|
|
if (!isset($values[$createdAtColumn]) && !isset($this->model->$createdAtColumn)) { |
85
|
39 |
|
$timestamps[$createdAtColumn] = $timestamp; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$values = array_merge( |
89
|
|
|
$timestamps, |
90
|
39 |
|
$values |
91
|
39 |
|
); |
92
|
|
|
|
93
|
|
|
return $values; |
94
|
39 |
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Add the "updated at" column to an array of values. |
98
|
|
|
* |
99
|
|
|
* @param array<string> $values |
100
|
|
|
* @return array<string> |
101
|
|
|
*/ |
102
|
|
|
protected function addUpdatedAtColumn(array $values): array |
103
|
|
|
{ |
104
|
39 |
|
if ( |
105
|
|
|
!$this->model->usesTimestamps() || |
106
|
|
|
is_null($this->model->getUpdatedAtColumn()) |
107
|
39 |
|
) { |
108
|
39 |
|
return $values; |
109
|
39 |
|
} |
110
|
|
|
|
111
|
|
|
$column = $this->model->getUpdatedAtColumn(); |
112
|
|
|
|
113
|
|
|
$values = array_merge( |
114
|
39 |
|
[$column => $this->model->freshTimestampString()], |
115
|
39 |
|
$values |
116
|
|
|
); |
117
|
39 |
|
|
118
|
39 |
|
return $values; |
119
|
|
|
} |
120
|
39 |
|
|
121
|
39 |
|
/** |
122
|
39 |
|
* Get the underlying query builder instance. |
123
|
|
|
* |
124
|
|
|
* @return QueryBuilder |
125
|
39 |
|
*/ |
126
|
39 |
|
public function getQuery() |
127
|
|
|
{ |
128
|
|
|
return $this->query; |
129
|
39 |
|
} |
130
|
|
|
} |
131
|
|
|
|