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