Skip to content

Commit 9431457

Browse files
committed
Tweak belongsToMany relation
1 parent 12abfb3 commit 9431457

File tree

2 files changed

+63
-12
lines changed

2 files changed

+63
-12
lines changed

src/Jenssegers/Mongodb/Relations/BelongsToMany.php

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php namespace Jenssegers\Mongodb\Relations;
22

3-
use Jenssegers\Mongodb\Model;
3+
use Illuminate\Database\Eloquent\Model;
44
use Illuminate\Database\Eloquent\Collection;
55
use Illuminate\Database\Eloquent\Relations\BelongsToMany as EloquentBelongsToMany;
66

@@ -40,6 +40,45 @@ public function addConstraints()
4040
}
4141
}
4242

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+
4382
/**
4483
* Sync the intermediate tables with a list of IDs or collection of models.
4584
*
@@ -102,7 +141,7 @@ public function sync($ids, $detaching = true)
102141
*/
103142
public function updateExistingPivot($id, array $attributes, $touch = true)
104143
{
105-
// TODO
144+
// Do nothing, we have no pivot table.
106145
}
107146

108147
/**
@@ -115,7 +154,10 @@ public function updateExistingPivot($id, array $attributes, $touch = true)
115154
*/
116155
public function attach($id, array $attributes = array(), $touch = true)
117156
{
118-
if ($id instanceof Model) $id = $id->getKey();
157+
if ($id instanceof Model)
158+
{
159+
$model = $id; $id = $model->getKey();
160+
}
119161

120162
$records = $this->createAttachRecords((array) $id, $attributes);
121163

@@ -126,14 +168,23 @@ public function attach($id, array $attributes = array(), $touch = true)
126168
// Attach the new ids to the parent model.
127169
$this->parent->push($this->otherKey, $otherIds, true);
128170

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();
131182

132-
// Set contraints on the related query.
133-
$query->where($this->related->getKeyName(), $id);
183+
$query->where($this->related->getKeyName(), $id);
134184

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+
}
137188

138189
if ($touch) $this->touchIfTouching();
139190
}

tests/RelationsTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -455,20 +455,20 @@ public function testDoubleSaveManyToMany()
455455
$user->save();
456456

457457
$this->assertEquals(1, $user->clients()->count());
458-
//$this->assertEquals(array($user->_id), $client->user_ids); TODO
458+
$this->assertEquals(array($user->_id), $client->user_ids);
459459
$this->assertEquals(array($client->_id), $user->client_ids);
460460

461461
$user = User::where('name', 'John Doe')->first();
462462
$client = Client::where('name', 'Admins')->first();
463463
$this->assertEquals(1, $user->clients()->count());
464-
//$this->assertEquals(array($user->_id), $client->user_ids); TODO
464+
$this->assertEquals(array($user->_id), $client->user_ids);
465465
$this->assertEquals(array($client->_id), $user->client_ids);
466466

467467
$user->clients()->save($client);
468468
$user->clients()->save($client);
469469
$user->save();
470470
$this->assertEquals(1, $user->clients()->count());
471-
//$this->assertEquals(array($user->_id), $client->user_ids); TODO
471+
$this->assertEquals(array($user->_id), $client->user_ids);
472472
$this->assertEquals(array($client->_id), $user->client_ids);
473473
}
474474

0 commit comments

Comments
 (0)