1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MaksimM\CompositePrimaryKeys\Eloquent\Relationships; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Collection; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
8
|
|
|
use Illuminate\Support\Collection as BaseCollection; |
9
|
|
|
use MaksimM\CompositePrimaryKeys\Http\Traits\CompositeRelationships; |
10
|
|
|
|
11
|
|
|
class CompositeBelongsToMany extends BelongsToMany |
12
|
|
|
{ |
13
|
|
|
use CompositeRelationships; |
14
|
|
|
|
15
|
|
|
private function normalizeIds($ids){ |
16
|
|
|
return $this->getQuery()->getModel()->hexBinaryColumns($this->relatedKey) ? (is_array($ids) ? array_map(function($id){ |
17
|
|
|
return $this->getQuery()->getModel()->recoverBinaryKey($this->relatedKey, $id); |
18
|
|
|
}, $ids) : $this->getQuery()->getModel()->recoverBinaryKey($this->relatedKey, $ids)) : $ids; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Get all of the IDs from the given mixed value. |
23
|
|
|
* |
24
|
|
|
* @param mixed $value |
25
|
|
|
* |
26
|
|
|
* @return array |
27
|
|
|
*/ |
28
|
|
|
protected function parseIds($value) |
29
|
|
|
{ |
30
|
|
|
if ($value instanceof Model) { |
31
|
|
|
return [$this->executeWithinOptionalBinaryTransformation(function () use ($value) { |
32
|
|
|
return $value->{$this->relatedKey}; |
33
|
|
|
}, $value)]; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if ($value instanceof Collection) { |
37
|
|
|
return $this->normalizeIds($value->pluck($this->relatedKey)->all()); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if ($value instanceof BaseCollection) { |
41
|
|
|
return $this->normalizeIds($value->toArray()); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return ctype_xdigit($value) ? $this->normalizeIds([$value]) : [$value]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Get the ID from the given mixed value. |
49
|
|
|
* |
50
|
|
|
* @param mixed $value |
51
|
|
|
* |
52
|
|
|
* @return mixed |
53
|
|
|
*/ |
54
|
|
|
protected function parseId($value) |
55
|
|
|
{ |
56
|
|
|
return $value instanceof Model ? $this->executeWithinOptionalBinaryTransformation(function () use ($value) { |
57
|
|
|
$value->{$this->relatedKey}; |
58
|
|
|
}, $value) : $value; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Create a new query builder for the pivot table. |
63
|
|
|
* |
64
|
|
|
* @return \Illuminate\Database\Query\Builder |
65
|
|
|
*/ |
66
|
|
|
protected function newPivotQuery() |
67
|
|
|
{ |
68
|
|
|
$query = $this->newPivotStatement(); |
69
|
|
|
|
70
|
|
|
foreach ($this->pivotWheres as $arguments) { |
71
|
|
|
call_user_func_array([$query, 'where'], $arguments); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
foreach ($this->pivotWhereIns as $arguments) { |
75
|
|
|
call_user_func_array([$query, 'whereIn'], $arguments); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $this->executeWithinOptionalBinaryTransformation(function () use ($query) { |
79
|
|
|
return $query->where($this->foreignPivotKey, $this->parent->{$this->parentKey}); |
80
|
|
|
}, $this->parent); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Create a new pivot attachment record. |
85
|
|
|
* |
86
|
|
|
* @param int $id |
87
|
|
|
* @param bool $timed |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
protected function baseAttachRecord($id, $timed) |
91
|
|
|
{ |
92
|
|
|
$record[$this->relatedPivotKey] = $id; |
|
|
|
|
93
|
|
|
|
94
|
|
|
$record[$this->foreignPivotKey] = $this->executeWithinOptionalBinaryTransformation(function () { |
95
|
|
|
return $this->parent->{$this->parentKey}; |
96
|
|
|
}, $this->parent); |
97
|
|
|
|
98
|
|
|
// If the record needs to have creation and update timestamps, we will make |
99
|
|
|
// them by calling the parent model's "freshTimestamp" method which will |
100
|
|
|
// provide us with a fresh timestamp in this model's preferred format. |
101
|
|
|
if ($timed) { |
102
|
|
|
$record = $this->addTimestampsToAttachment($record); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
foreach ($this->pivotValues as $value) { |
106
|
|
|
$record[$value['column']] = $value['value']; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $record; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|