Skip to content

Commit 0caeeb9

Browse files
Add the associate method to EmbedsMany and refactoring
1 parent a86d16a commit 0caeeb9

File tree

2 files changed

+89
-22
lines changed

2 files changed

+89
-22
lines changed

src/Jenssegers/Mongodb/Relations/EmbedsMany.php

Lines changed: 68 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ public function get()
140140
*/
141141
public function save(Model $model)
142142
{
143+
$this->updateTimestamps($model);
144+
143145
// Insert a new document.
144146
if ( ! $model->exists)
145147
{
@@ -154,34 +156,40 @@ public function save(Model $model)
154156
}
155157

156158
/**
157-
* Perform a model insert operation.
159+
* Attach a model instance to the parent model without persistence.
158160
*
159161
* @param \Illuminate\Database\Eloquent\Model $model
160162
* @return \Illuminate\Database\Eloquent\Model
161163
*/
162-
protected function performInsert(Model $model)
164+
public function associate(Model $model)
163165
{
164-
// Create a new key.
165-
if ( ! $model->getAttribute('_id'))
166+
// Insert the related model in the parent instance
167+
if ( ! $model->exists)
166168
{
167-
$model->setAttribute('_id', new MongoId);
169+
return $this->associateNew($model);
168170
}
169171

170-
// Update timestamps.
171-
$this->updateTimestamps($model);
172-
173-
// Push the document to the database.
174-
$result = $this->query->push($this->localKey, $model->getAttributes(), true);
175-
176-
$documents = $this->getEmbeddedRecords();
172+
// Update the related model in the parent instance
173+
else
174+
{
175+
return $this->associateExisting($model);
176+
}
177177

178-
// Add the document to the parent model.
179-
$documents[] = $model->getAttributes();
178+
}
180179

181-
$this->setEmbeddedRecords($documents);
180+
/**
181+
* Perform a model insert operation.
182+
*
183+
* @param \Illuminate\Database\Eloquent\Model $model
184+
* @return \Illuminate\Database\Eloquent\Model
185+
*/
186+
protected function performInsert(Model $model)
187+
{
188+
// Insert the related model in the parent instance
189+
$this->associateNew($model);
182190

183-
// Mark the model as existing.
184-
$model->exists = true;
191+
// Push the document to the database.
192+
$result = $this->query->push($this->localKey, $model->getAttributes(), true);
185193

186194
return $result ? $model : false;
187195
}
@@ -194,8 +202,8 @@ protected function performInsert(Model $model)
194202
*/
195203
protected function performUpdate(Model $model)
196204
{
197-
// Update timestamps.
198-
$this->updateTimestamps($model);
205+
// Update the related model in the parent instance
206+
$this->associateExisting($model);
199207

200208
// Get the correct foreign key value.
201209
$id = $this->getForeignKeyValue($model->getKey());
@@ -204,6 +212,44 @@ protected function performUpdate(Model $model)
204212
$result = $this->query->where($this->localKey . '.' . $model->getKeyName(), $id)
205213
->update(array($this->localKey . '.$' => $model->getAttributes()));
206214

215+
return $result ? $model : false;
216+
}
217+
218+
/**
219+
* Attach a new model without persistence
220+
*
221+
* @param \Illuminate\Database\Eloquent\Model $model
222+
* @return \Illuminate\Database\Eloquent\Model
223+
*/
224+
protected function associateNew($model)
225+
{
226+
// Create a new key.
227+
if ( ! $model->getAttribute('_id'))
228+
{
229+
$model->setAttribute('_id', new MongoId);
230+
}
231+
232+
$documents = $this->getEmbeddedRecords();
233+
234+
// Add the document to the parent model.
235+
$documents[] = $model->getAttributes();
236+
237+
$this->setEmbeddedRecords($documents);
238+
239+
// Mark the model as existing.
240+
$model->exists = true;
241+
242+
return $model;
243+
}
244+
245+
/**
246+
* Update an existing model without persistence
247+
*
248+
* @param \Illuminate\Database\Eloquent\Model $model
249+
* @return \Illuminate\Database\Eloquent\Model
250+
*/
251+
protected function associateExisting($model)
252+
{
207253
// Get existing embedded documents.
208254
$documents = $this->getEmbeddedRecords();
209255

@@ -212,18 +258,18 @@ protected function performUpdate(Model $model)
212258
$key = $model->getKey();
213259

214260
// Replace the document in the parent model.
215-
foreach ($documents as $i => $document)
261+
foreach ($documents as &$document)
216262
{
217263
if ($document[$primaryKey] == $key)
218264
{
219-
$documents[$i] = $model->getAttributes();
265+
$document = $model->getAttributes();
220266
break;
221267
}
222268
}
223269

224270
$this->setEmbeddedRecords($documents);
225271

226-
return $result ? $model : false;
272+
return $model;
227273
}
228274

229275
/**

tests/RelationsTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,27 @@ public function testEmbedsManySave()
329329
$this->assertEquals(array('London', 'Manhattan', 'Bruxelles'), $freshUser->addresses->lists('city'));
330330
}
331331

332+
public function testEmbedsManyAssociate()
333+
{
334+
$user = User::create(array('name' => 'John Doe'));
335+
$address = new Address(array('city' => 'London'));
336+
337+
$address = $user->addresses()->associate($address);
338+
$this->assertNotNull($user->_addresses);
339+
$this->assertEquals(array('London'), $user->addresses->lists('city'));
340+
$this->assertNotNull($address->_id);
341+
342+
$freshUser = User::find($user->_id);
343+
$this->assertEquals(array(), $freshUser->addresses->lists('city'));
344+
345+
$address->city = 'Londinium';
346+
$user->addresses()->associate($address);
347+
$this->assertEquals(array('Londinium'), $user->addresses->lists('city'));
348+
349+
$freshUser = User::find($user->_id);
350+
$this->assertEquals(array(), $freshUser->addresses->lists('city'));
351+
}
352+
332353
public function testEmbedsManySaveMany()
333354
{
334355
$user = User::create(array('name' => 'John Doe'));

0 commit comments

Comments
 (0)