1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace LaravelFreelancerNL\Aranguent\Eloquent; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder as IlluminateBuilder; |
6
|
|
|
|
7
|
|
|
class Builder extends IlluminateBuilder |
8
|
|
|
{ |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* The methods that should be returned from query builder. |
12
|
|
|
* |
13
|
|
|
* @var array |
14
|
|
|
*/ |
15
|
|
|
protected $passthru = [ |
16
|
|
|
'insert', 'insertOrIgnore', 'insertGetId', 'insertUsing', 'getBindings', 'toSql', 'dump', 'dd', |
17
|
|
|
'exists', 'doesntExist', 'count', 'min', 'max', 'avg', 'average', 'sum', 'getConnection', |
18
|
|
|
]; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Update a record in the database. |
23
|
|
|
* |
24
|
|
|
* @param array $values |
25
|
|
|
* @return int |
26
|
|
|
*/ |
27
|
|
|
public function update(array $values) |
28
|
|
|
{ |
29
|
|
|
return $this->toBase()->update($this->updateTimestamps($values)); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Update a record in the database. |
34
|
|
|
* |
35
|
|
|
* @param array $values |
36
|
|
|
* @return int |
37
|
|
|
*/ |
38
|
|
|
public function insert(array $values) |
39
|
|
|
{ |
40
|
|
|
// Since every insert gets treated like a batch insert, we will make sure the |
41
|
|
|
// bindings are structured in a way that is convenient when building these |
42
|
|
|
// inserts statements by verifying these elements are actually an array. |
43
|
|
|
if (empty($values)) { |
44
|
|
|
return true; |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if (! is_array(reset($values))) { |
48
|
|
|
$values = [$values]; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
// Here, we will sort the insert keys for every record so that each insert is |
52
|
|
|
// in the same order for the record. We need to make sure this is the case |
53
|
|
|
// so there are not any errors or problems when inserting these records. |
54
|
|
|
else { |
55
|
|
|
foreach ($values as $key => $value) { |
56
|
|
|
ksort($value); |
57
|
|
|
|
58
|
|
|
$values[$key] = $value; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
//Set timestamps |
63
|
|
|
foreach ($values as $key => $value) { |
64
|
|
|
$values[$key] = $this->updateTimestamps($value); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $this->toBase()->insert($values); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Add the "updated at" column to an array of values. |
72
|
|
|
* |
73
|
|
|
* @param array $values |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
protected function updateTimestamps(array $values) |
77
|
|
|
{ |
78
|
|
|
if (! $this->model->usesTimestamps() || |
79
|
|
|
is_null($this->model->getUpdatedAtColumn()) || |
80
|
|
|
is_null($this->model->getCreatedAtColumn())) { |
81
|
|
|
return $values; |
82
|
|
|
} |
83
|
|
|
$timestamp = $this->model->freshTimestampString(); |
84
|
|
|
$updatedAtColumn = $this->model->getUpdatedAtColumn(); |
85
|
|
|
$createdAtColumn = $this->model->getCreatedAtColumn(); |
86
|
|
|
$timestamps[$updatedAtColumn] = $timestamp; |
|
|
|
|
87
|
|
|
if (! isset($values[$createdAtColumn])) { |
88
|
|
|
$timestamps[$createdAtColumn] = $timestamp; |
89
|
|
|
} |
90
|
|
|
$values = array_merge( |
91
|
|
|
$timestamps, |
92
|
|
|
$values |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
return $values; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
|
99
|
|
|
} |
100
|
|
|
|