1
1
<?php namespace Jenssegers \Mongodb \Relations ;
2
2
3
- use Jenssegers \ Mongodb \Model ;
3
+ use Illuminate \ Database \ Eloquent \Model ;
4
4
use Illuminate \Database \Eloquent \Collection ;
5
5
use Illuminate \Database \Eloquent \Relations \BelongsToMany as EloquentBelongsToMany ;
6
6
@@ -40,6 +40,45 @@ public function addConstraints()
40
40
}
41
41
}
42
42
43
+ /**
44
+ * Save a new model and attach it to the parent model.
45
+ *
46
+ * @param \Illuminate\Database\Eloquent\Model $model
47
+ * @param array $joining
48
+ * @param bool $touch
49
+ * @return \Illuminate\Database\Eloquent\Model
50
+ */
51
+ public function save (Model $ model , array $ joining = array (), $ touch = true )
52
+ {
53
+ $ model ->save (array ('touch ' => false ));
54
+
55
+ $ this ->attach ($ model , $ joining , $ touch );
56
+
57
+ return $ model ;
58
+ }
59
+
60
+ /**
61
+ * Create a new instance of the related model.
62
+ *
63
+ * @param array $attributes
64
+ * @param array $joining
65
+ * @param bool $touch
66
+ * @return \Illuminate\Database\Eloquent\Model
67
+ */
68
+ public function create (array $ attributes , array $ joining = array (), $ touch = true )
69
+ {
70
+ $ instance = $ this ->related ->newInstance ($ attributes );
71
+
72
+ // Once we save the related model, we need to attach it to the base model via
73
+ // through intermediate table so we'll use the existing "attach" method to
74
+ // accomplish this which will insert the record and any more attributes.
75
+ $ instance ->save (array ('touch ' => false ));
76
+
77
+ $ this ->attach ($ instance , $ joining , $ touch );
78
+
79
+ return $ instance ;
80
+ }
81
+
43
82
/**
44
83
* Sync the intermediate tables with a list of IDs or collection of models.
45
84
*
@@ -102,7 +141,7 @@ public function sync($ids, $detaching = true)
102
141
*/
103
142
public function updateExistingPivot ($ id , array $ attributes , $ touch = true )
104
143
{
105
- // TODO
144
+ // Do nothing, we have no pivot table.
106
145
}
107
146
108
147
/**
@@ -115,7 +154,10 @@ public function updateExistingPivot($id, array $attributes, $touch = true)
115
154
*/
116
155
public function attach ($ id , array $ attributes = array (), $ touch = true )
117
156
{
118
- if ($ id instanceof Model) $ id = $ id ->getKey ();
157
+ if ($ id instanceof Model)
158
+ {
159
+ $ model = $ id ; $ id = $ model ->getKey ();
160
+ }
119
161
120
162
$ records = $ this ->createAttachRecords ((array ) $ id , $ attributes );
121
163
@@ -126,14 +168,23 @@ public function attach($id, array $attributes = array(), $touch = true)
126
168
// Attach the new ids to the parent model.
127
169
$ this ->parent ->push ($ this ->otherKey , $ otherIds , true );
128
170
129
- // Generate a new related query instance.
130
- $ query = $ this ->newRelatedQuery ();
171
+ // If we have a model instance, we can psuh the ids to that model,
172
+ // so that the internal attributes are updated as well. Otherwise,
173
+ // we will just perform a regular database query.
174
+ if (isset ($ model ))
175
+ {
176
+ // Attach the new ids to the related model.
177
+ $ model ->push ($ this ->foreignKey , $ foreignIds , true );
178
+ }
179
+ else
180
+ {
181
+ $ query = $ this ->newRelatedQuery ();
131
182
132
- // Set contraints on the related query.
133
- $ query ->where ($ this ->related ->getKeyName (), $ id );
183
+ $ query ->where ($ this ->related ->getKeyName (), $ id );
134
184
135
- // Attach the new ids to the related model.
136
- $ query ->push ($ this ->foreignKey , $ foreignIds , true );
185
+ // Attach the new ids to the related model.
186
+ $ query ->push ($ this ->foreignKey , $ foreignIds , true );
187
+ }
137
188
138
189
if ($ touch ) $ this ->touchIfTouching ();
139
190
}
0 commit comments