1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @author Jared King <[email protected]> |
5
|
|
|
* |
6
|
|
|
* @link http://jaredtking.com |
7
|
|
|
* |
8
|
|
|
* @copyright 2015 Jared King |
9
|
|
|
* @license MIT |
10
|
|
|
*/ |
11
|
|
|
namespace Pulsar\Relation; |
12
|
|
|
|
13
|
|
|
use Pulsar\Model; |
14
|
|
|
|
15
|
|
|
class BelongsToMany extends Relation |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $tablename; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param Model $localModel |
24
|
|
|
* @param string $localKey identifying key on local model |
25
|
|
|
* @param string $tablename pivot table name |
26
|
|
|
* @param string $foreignModel foreign model class |
27
|
|
|
* @param string $foreignKey identifying key on foreign model |
28
|
|
|
*/ |
29
|
|
|
public function __construct(Model $localModel, $localKey, $tablename, $foreignModel, $foreignKey) |
30
|
|
|
{ |
31
|
|
|
$this->tablename = $tablename; |
32
|
|
|
|
33
|
|
|
parent::__construct($localModel, $localKey, $foreignModel, $foreignKey); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
protected function initQuery() |
37
|
|
|
{ |
38
|
|
|
$pivot = new Pivot(); |
39
|
|
|
$pivot->setTablename($this->tablename); |
40
|
|
|
|
41
|
|
|
$ids = $this->localModel->ids(); |
42
|
|
|
foreach ($ids as $idProperty => $id) { |
43
|
|
|
if ($id === null) { |
44
|
|
|
$this->empty = true; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
$this->query->where($this->localKey, $id); |
48
|
|
|
$this->query->join($pivot, $this->foreignKey, $idProperty); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function getResults() |
53
|
|
|
{ |
54
|
|
|
if ($this->empty) { |
55
|
|
|
return; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $this->query->execute(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Gets the pivot tablename. |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
public function getTablename() |
67
|
|
|
{ |
68
|
|
|
return $this->tablename; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
View Code Duplication |
public function create(array $values = []) |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$class = $this->foreignModel; |
74
|
|
|
$model = new $class(); |
75
|
|
|
$model->create($values); |
76
|
|
|
|
77
|
|
|
$this->attach($model); |
78
|
|
|
|
79
|
|
|
return $model; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Attaches a model to the relationship by creating |
84
|
|
|
* a pivot model. |
85
|
|
|
* |
86
|
|
|
* @param Model $model |
87
|
|
|
* |
88
|
|
|
* @return self |
89
|
|
|
*/ |
90
|
|
|
public function attach(Model $model) |
91
|
|
|
{ |
92
|
|
|
// create pivot relation |
93
|
|
|
$pivot = new Pivot(); |
94
|
|
|
$pivot->setTablename($this->tablename); |
95
|
|
|
|
96
|
|
|
// build the local side |
97
|
|
|
$ids = $this->localModel->ids(); |
98
|
|
|
foreach ($ids as $property => $id) { |
99
|
|
|
$pivot->{$this->localKey} = $id; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
// build the foreign side |
103
|
|
|
$ids = $model->ids(); |
104
|
|
|
foreach ($ids as $property => $id) { |
105
|
|
|
$pivot->{$this->foreignKey} = $id; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$pivot->save(); |
109
|
|
|
$model->pivot = $pivot; |
|
|
|
|
110
|
|
|
|
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Detaches a model from the relationship by deleting |
116
|
|
|
* the pivot model. |
117
|
|
|
* |
118
|
|
|
* @param Model $model |
119
|
|
|
* |
120
|
|
|
* @return self |
121
|
|
|
*/ |
122
|
|
|
public function detach(Model $model) |
123
|
|
|
{ |
124
|
|
|
$model->pivot->delete(); |
|
|
|
|
125
|
|
|
unset($model->pivot); |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.