Skip to content

Commit 53c17fb

Browse files
committed
Mergin with master
2 parents b358f63 + 9829d49 commit 53c17fb

File tree

6 files changed

+107
-25
lines changed

6 files changed

+107
-25
lines changed

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ If you are using a different database driver as the default one, you will need t
7878
Everything else works just like the original Eloquent model. Read more about the Eloquent on http://laravel.com/docs/eloquent
7979

8080
### Optional: Alias
81-
-------------------
8281

8382
You may also register an alias for the MongoDB model by adding the following to the alias array in `app/config/app.php`:
8483

@@ -268,8 +267,6 @@ Supported relations are:
268267
- belongsTo
269268
- belongsToMany
270269

271-
*The belongsToMany relation will not use a pivot "table", but will push id's to a **related_ids** attribute instead.*
272-
273270
Example:
274271

275272
use Jenssegers\Mongodb\Model as Eloquent;
@@ -296,6 +293,19 @@ And the inverse relation:
296293

297294
}
298295

296+
The belongsToMany relation will not use a pivot "table", but will push id's to a __related_ids__ attribute instead. This makes the second parameter for the belongsToMany method useless. If you want to define custom keys for your relation, set it to `null`:
297+
298+
use Jenssegers\Mongodb\Model as Eloquent;
299+
300+
class User extends Eloquent {
301+
302+
public function groups()
303+
{
304+
return $this->belongsToMany('Group', null, 'users', 'groups');
305+
}
306+
307+
}
308+
299309
Other relations are not yet supported, but may be added in the future. Read more about these relations on http://four.laravel.com/docs/eloquent#relationships
300310

301311
### Raw Expressions

src/Jenssegers/Mongodb/Relations/BelongsToMany.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ public function attach($id, array $attributes = array(), $touch = true)
144144
*/
145145
protected function createAttachRecords($ids, array $attributes)
146146
{
147-
$records = array();;
147+
$records = array();
148148

149149
// To create the attachment records, we will simply spin through the IDs given
150150
// and create a new record to insert for each ID. Each ID may actually be a
@@ -170,6 +170,9 @@ public function detach($ids = array(), $touch = true)
170170

171171
$query = $this->newParentQuery();
172172

173+
// Generate a new related query instance
174+
$related = $this->related->newInstance();
175+
173176
// If associated IDs were passed to the method we will only delete those
174177
// associations, otherwise all of the association ties will be broken.
175178
// We'll return the numbers of affected rows when we do the deletes.
@@ -185,10 +188,13 @@ public function detach($ids = array(), $touch = true)
185188
// Once we have all of the conditions set on the statement, we are ready
186189
// to run the delete on the pivot table. Then, if the touch parameter
187190
// is true, we will go ahead and touch all related models to sync.
188-
foreach($ids as $id)
191+
foreach ($ids as $id)
189192
{
190193
$query->pull($this->otherKey, $id);
191194
}
195+
196+
// Remove the relation from the related model
197+
$related->pull($this->foreignKey, $this->parent->getKey());
192198

193199
return count($ids);
194200
}

tests/RelationsTest.php

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ public function tearDown()
1212
Item::truncate();
1313
Role::truncate();
1414
Client::truncate();
15+
Group::truncate();
1516
}
1617

1718
public function testHasMany()
@@ -126,17 +127,22 @@ public function testEasyRelation()
126127
$this->assertEquals('admin', $role->type);
127128
}
128129

129-
public function testHasManyAndBelongsTo()
130+
public function testBelongsToMany()
130131
{
131132
$user = User::create(array('name' => 'John Doe'));
132133

134+
// Add 2 clients
133135
$user->clients()->save(new Client(array('name' => 'Pork Pies Ltd.')));
134136
$user->clients()->create(array('name' => 'Buffet Bar Inc.'));
135137

138+
// Refetch
136139
$user = User::with('clients')->find($user->_id);
137-
138140
$client = Client::with('users')->first();
139141

142+
// Check for relation attributes
143+
$this->assertTrue(array_key_exists('user_ids', $client->getAttributes()));
144+
$this->assertTrue(array_key_exists('client_ids', $user->getAttributes()));
145+
140146
$clients = $client->getRelation('users');
141147
$users = $user->getRelation('clients');
142148

@@ -148,15 +154,13 @@ public function testHasManyAndBelongsTo()
148154
$this->assertCount(1, $client->users);
149155

150156
// Now create a new user to an existing client
151-
$client->users()->create(array('name' => 'Jane Doe'));
157+
$user = $client->users()->create(array('name' => 'Jane Doe'));
152158

153-
$otherClient = User::where('name', '=', 'Jane Doe')->first()->clients()->get();
159+
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $user->clients);
160+
$this->assertInstanceOf('Client', $user->clients->first());
161+
$this->assertCount(1, $user->clients);
154162

155-
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $otherClient);
156-
$this->assertInstanceOf('Client', $otherClient[0]);
157-
$this->assertCount(1, $otherClient);
158-
159-
// Now attach an existing client to an existing user
163+
// Get user and unattached client
160164
$user = User::where('name', '=', 'Jane Doe')->first();
161165
$client = Client::Where('name', '=', 'Buffet Bar Inc.')->first();
162166

@@ -167,6 +171,8 @@ public function testHasManyAndBelongsTo()
167171
// Assert they are not attached
168172
$this->assertFalse(in_array($client->_id, $user->client_ids));
169173
$this->assertFalse(in_array($user->_id, $client->user_ids));
174+
$this->assertCount(1, $user->clients);
175+
$this->assertCount(1, $client->users);
170176

171177
// Attach the client to the user
172178
$user->clients()->attach($client);
@@ -178,9 +184,24 @@ public function testHasManyAndBelongsTo()
178184
// Assert they are attached
179185
$this->assertTrue(in_array($client->_id, $user->client_ids));
180186
$this->assertTrue(in_array($user->_id, $client->user_ids));
187+
$this->assertCount(2, $user->clients);
188+
$this->assertCount(2, $client->users);
189+
190+
// Detach clients from user
191+
$user->clients()->sync(array());
192+
193+
// Get the new user model
194+
$user = User::where('name', '=', 'Jane Doe')->first();
195+
$client = Client::Where('name', '=', 'Buffet Bar Inc.')->first();
196+
197+
// Assert they are not attached
198+
$this->assertFalse(in_array($client->_id, $user->client_ids));
199+
$this->assertFalse(in_array($user->_id, $client->user_ids));
200+
$this->assertCount(0, $user->clients);
201+
$this->assertCount(1, $client->users);
181202
}
182203

183-
public function testHasManyAndBelongsToAttachesExistingModels()
204+
public function testBelongsToManyAttachesExistingModels()
184205
{
185206
$user = User::create(array('name' => 'John Doe', 'client_ids' => array('1234523')));
186207

@@ -190,8 +211,8 @@ public function testHasManyAndBelongsToAttachesExistingModels()
190211
);
191212

192213
$moreClients = array(
193-
Client::create(array('name' => 'Boloni Ltd.'))->_id,
194-
Client::create(array('name' => 'Meatballs Inc.'))->_id
214+
Client::create(array('name' => 'synced Boloni Ltd.'))->_id,
215+
Client::create(array('name' => 'synced Meatballs Inc.'))->_id
195216
);
196217

197218
// Sync multiple records
@@ -205,11 +226,37 @@ public function testHasManyAndBelongsToAttachesExistingModels()
205226
// Assert there are two client objects in the relationship
206227
$this->assertCount(2, $user->clients);
207228

229+
// Add more clients
208230
$user->clients()->sync($moreClients);
209231

232+
// Refetch
210233
$user = User::with('clients')->find($user->_id);
211234

212-
// Assert there are now 4 client objects in the relationship
213-
$this->assertCount(4, $user->clients);
235+
// Assert there are now still 2 client objects in the relationship
236+
$this->assertCount(2, $user->clients);
237+
238+
// Assert that the new relationships name start with synced
239+
$this->assertStringStartsWith('synced', $user->clients[0]->name);
240+
$this->assertStringStartsWith('synced', $user->clients[1]->name);
241+
}
242+
243+
public function testBelongsToManyCustom()
244+
{
245+
$user = User::create(array('name' => 'John Doe'));
246+
$group = $user->groups()->create(array('name' => 'Admins'));
247+
248+
// Refetch
249+
$user = User::find($user->_id);
250+
$group = Group::find($group->_id);
251+
252+
// Check for custom relation attributes
253+
$this->assertTrue(array_key_exists('users', $group->getAttributes()));
254+
$this->assertTrue(array_key_exists('groups', $user->getAttributes()));
255+
256+
// Assert they are attached
257+
$this->assertTrue(in_array($group->_id, $user->groups));
258+
$this->assertTrue(in_array($user->_id, $group->users));
259+
$this->assertEquals($group->_id, $user->groups()->first()->_id);
260+
$this->assertEquals($user->_id, $group->users()->first()->_id);
214261
}
215262
}

tests/models/Client.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ class Client extends Eloquent {
66
protected $collection = 'clients';
77

88
protected static $unguarded = true;
9-
9+
1010
public function users()
1111
{
1212
return $this->belongsToMany('User');
1313
}
14-
}
14+
}

tests/models/Group.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
use Jenssegers\Mongodb\Model as Eloquent;
3+
4+
class Group extends Eloquent {
5+
6+
protected $collection = 'groups';
7+
8+
protected static $unguarded = true;
9+
10+
public function users()
11+
{
12+
return $this->belongsToMany('User', null, 'groups', 'users');
13+
}
14+
}

tests/models/User.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
class User extends Eloquent implements UserInterface, RemindableInterface {
99

1010
protected $collection = 'users';
11-
11+
1212
protected $dates = array('birthday');
13-
13+
1414
protected static $unguarded = true;
1515

1616
public function books()
@@ -27,12 +27,17 @@ public function role()
2727
{
2828
return $this->hasOne('Role');
2929
}
30-
30+
3131
public function clients()
3232
{
3333
return $this->belongsToMany('Client');
3434
}
3535

36+
public function groups()
37+
{
38+
return $this->belongsToMany('Group', null, 'users', 'groups');
39+
}
40+
3641
/**
3742
* Get the unique identifier for the user.
3843
*
@@ -62,4 +67,4 @@ public function getReminderEmail()
6267
{
6368
return $this->email;
6469
}
65-
}
70+
}

0 commit comments

Comments
 (0)