Commit 53c17fb4 authored by Jens Segers's avatar Jens Segers

Mergin with master

parents b358f631 9829d49e
...@@ -78,7 +78,6 @@ If you are using a different database driver as the default one, you will need t ...@@ -78,7 +78,6 @@ If you are using a different database driver as the default one, you will need t
Everything else works just like the original Eloquent model. Read more about the Eloquent on http://laravel.com/docs/eloquent Everything else works just like the original Eloquent model. Read more about the Eloquent on http://laravel.com/docs/eloquent
### Optional: Alias ### Optional: Alias
-------------------
You may also register an alias for the MongoDB model by adding the following to the alias array in `app/config/app.php`: You may also register an alias for the MongoDB model by adding the following to the alias array in `app/config/app.php`:
...@@ -268,8 +267,6 @@ Supported relations are: ...@@ -268,8 +267,6 @@ Supported relations are:
- belongsTo - belongsTo
- belongsToMany - belongsToMany
*The belongsToMany relation will not use a pivot "table", but will push id's to a **related_ids** attribute instead.*
Example: Example:
use Jenssegers\Mongodb\Model as Eloquent; use Jenssegers\Mongodb\Model as Eloquent;
...@@ -296,6 +293,19 @@ And the inverse relation: ...@@ -296,6 +293,19 @@ And the inverse relation:
} }
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`:
use Jenssegers\Mongodb\Model as Eloquent;
class User extends Eloquent {
public function groups()
{
return $this->belongsToMany('Group', null, 'users', 'groups');
}
}
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 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
### Raw Expressions ### Raw Expressions
......
...@@ -144,7 +144,7 @@ class BelongsToMany extends EloquentBelongsToMany { ...@@ -144,7 +144,7 @@ class BelongsToMany extends EloquentBelongsToMany {
*/ */
protected function createAttachRecords($ids, array $attributes) protected function createAttachRecords($ids, array $attributes)
{ {
$records = array();; $records = array();
// To create the attachment records, we will simply spin through the IDs given // To create the attachment records, we will simply spin through the IDs given
// and create a new record to insert for each ID. Each ID may actually be a // and create a new record to insert for each ID. Each ID may actually be a
...@@ -170,6 +170,9 @@ class BelongsToMany extends EloquentBelongsToMany { ...@@ -170,6 +170,9 @@ class BelongsToMany extends EloquentBelongsToMany {
$query = $this->newParentQuery(); $query = $this->newParentQuery();
// Generate a new related query instance
$related = $this->related->newInstance();
// If associated IDs were passed to the method we will only delete those // If associated IDs were passed to the method we will only delete those
// associations, otherwise all of the association ties will be broken. // associations, otherwise all of the association ties will be broken.
// We'll return the numbers of affected rows when we do the deletes. // We'll return the numbers of affected rows when we do the deletes.
...@@ -185,10 +188,13 @@ class BelongsToMany extends EloquentBelongsToMany { ...@@ -185,10 +188,13 @@ class BelongsToMany extends EloquentBelongsToMany {
// Once we have all of the conditions set on the statement, we are ready // Once we have all of the conditions set on the statement, we are ready
// to run the delete on the pivot table. Then, if the touch parameter // to run the delete on the pivot table. Then, if the touch parameter
// is true, we will go ahead and touch all related models to sync. // is true, we will go ahead and touch all related models to sync.
foreach($ids as $id) foreach ($ids as $id)
{ {
$query->pull($this->otherKey, $id); $query->pull($this->otherKey, $id);
} }
// Remove the relation from the related model
$related->pull($this->foreignKey, $this->parent->getKey());
return count($ids); return count($ids);
} }
......
...@@ -12,6 +12,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -12,6 +12,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
Item::truncate(); Item::truncate();
Role::truncate(); Role::truncate();
Client::truncate(); Client::truncate();
Group::truncate();
} }
public function testHasMany() public function testHasMany()
...@@ -126,17 +127,22 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -126,17 +127,22 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$this->assertEquals('admin', $role->type); $this->assertEquals('admin', $role->type);
} }
public function testHasManyAndBelongsTo() public function testBelongsToMany()
{ {
$user = User::create(array('name' => 'John Doe')); $user = User::create(array('name' => 'John Doe'));
// Add 2 clients
$user->clients()->save(new Client(array('name' => 'Pork Pies Ltd.'))); $user->clients()->save(new Client(array('name' => 'Pork Pies Ltd.')));
$user->clients()->create(array('name' => 'Buffet Bar Inc.')); $user->clients()->create(array('name' => 'Buffet Bar Inc.'));
// Refetch
$user = User::with('clients')->find($user->_id); $user = User::with('clients')->find($user->_id);
$client = Client::with('users')->first(); $client = Client::with('users')->first();
// Check for relation attributes
$this->assertTrue(array_key_exists('user_ids', $client->getAttributes()));
$this->assertTrue(array_key_exists('client_ids', $user->getAttributes()));
$clients = $client->getRelation('users'); $clients = $client->getRelation('users');
$users = $user->getRelation('clients'); $users = $user->getRelation('clients');
...@@ -148,15 +154,13 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -148,15 +154,13 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$this->assertCount(1, $client->users); $this->assertCount(1, $client->users);
// Now create a new user to an existing client // Now create a new user to an existing client
$client->users()->create(array('name' => 'Jane Doe')); $user = $client->users()->create(array('name' => 'Jane Doe'));
$otherClient = User::where('name', '=', 'Jane Doe')->first()->clients()->get(); $this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $user->clients);
$this->assertInstanceOf('Client', $user->clients->first());
$this->assertCount(1, $user->clients);
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $otherClient); // Get user and unattached client
$this->assertInstanceOf('Client', $otherClient[0]);
$this->assertCount(1, $otherClient);
// Now attach an existing client to an existing user
$user = User::where('name', '=', 'Jane Doe')->first(); $user = User::where('name', '=', 'Jane Doe')->first();
$client = Client::Where('name', '=', 'Buffet Bar Inc.')->first(); $client = Client::Where('name', '=', 'Buffet Bar Inc.')->first();
...@@ -167,6 +171,8 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -167,6 +171,8 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
// Assert they are not attached // Assert they are not attached
$this->assertFalse(in_array($client->_id, $user->client_ids)); $this->assertFalse(in_array($client->_id, $user->client_ids));
$this->assertFalse(in_array($user->_id, $client->user_ids)); $this->assertFalse(in_array($user->_id, $client->user_ids));
$this->assertCount(1, $user->clients);
$this->assertCount(1, $client->users);
// Attach the client to the user // Attach the client to the user
$user->clients()->attach($client); $user->clients()->attach($client);
...@@ -178,9 +184,24 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -178,9 +184,24 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
// Assert they are attached // Assert they are attached
$this->assertTrue(in_array($client->_id, $user->client_ids)); $this->assertTrue(in_array($client->_id, $user->client_ids));
$this->assertTrue(in_array($user->_id, $client->user_ids)); $this->assertTrue(in_array($user->_id, $client->user_ids));
$this->assertCount(2, $user->clients);
$this->assertCount(2, $client->users);
// Detach clients from user
$user->clients()->sync(array());
// Get the new user model
$user = User::where('name', '=', 'Jane Doe')->first();
$client = Client::Where('name', '=', 'Buffet Bar Inc.')->first();
// Assert they are not attached
$this->assertFalse(in_array($client->_id, $user->client_ids));
$this->assertFalse(in_array($user->_id, $client->user_ids));
$this->assertCount(0, $user->clients);
$this->assertCount(1, $client->users);
} }
public function testHasManyAndBelongsToAttachesExistingModels() public function testBelongsToManyAttachesExistingModels()
{ {
$user = User::create(array('name' => 'John Doe', 'client_ids' => array('1234523'))); $user = User::create(array('name' => 'John Doe', 'client_ids' => array('1234523')));
...@@ -190,8 +211,8 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -190,8 +211,8 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
); );
$moreClients = array( $moreClients = array(
Client::create(array('name' => 'Boloni Ltd.'))->_id, Client::create(array('name' => 'synced Boloni Ltd.'))->_id,
Client::create(array('name' => 'Meatballs Inc.'))->_id Client::create(array('name' => 'synced Meatballs Inc.'))->_id
); );
// Sync multiple records // Sync multiple records
...@@ -205,11 +226,37 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -205,11 +226,37 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
// Assert there are two client objects in the relationship // Assert there are two client objects in the relationship
$this->assertCount(2, $user->clients); $this->assertCount(2, $user->clients);
// Add more clients
$user->clients()->sync($moreClients); $user->clients()->sync($moreClients);
// Refetch
$user = User::with('clients')->find($user->_id); $user = User::with('clients')->find($user->_id);
// Assert there are now 4 client objects in the relationship // Assert there are now still 2 client objects in the relationship
$this->assertCount(4, $user->clients); $this->assertCount(2, $user->clients);
// Assert that the new relationships name start with synced
$this->assertStringStartsWith('synced', $user->clients[0]->name);
$this->assertStringStartsWith('synced', $user->clients[1]->name);
}
public function testBelongsToManyCustom()
{
$user = User::create(array('name' => 'John Doe'));
$group = $user->groups()->create(array('name' => 'Admins'));
// Refetch
$user = User::find($user->_id);
$group = Group::find($group->_id);
// Check for custom relation attributes
$this->assertTrue(array_key_exists('users', $group->getAttributes()));
$this->assertTrue(array_key_exists('groups', $user->getAttributes()));
// Assert they are attached
$this->assertTrue(in_array($group->_id, $user->groups));
$this->assertTrue(in_array($user->_id, $group->users));
$this->assertEquals($group->_id, $user->groups()->first()->_id);
$this->assertEquals($user->_id, $group->users()->first()->_id);
} }
} }
...@@ -6,9 +6,9 @@ class Client extends Eloquent { ...@@ -6,9 +6,9 @@ class Client extends Eloquent {
protected $collection = 'clients'; protected $collection = 'clients';
protected static $unguarded = true; protected static $unguarded = true;
public function users() public function users()
{ {
return $this->belongsToMany('User'); return $this->belongsToMany('User');
} }
} }
\ No newline at end of file
<?php
use Jenssegers\Mongodb\Model as Eloquent;
class Group extends Eloquent {
protected $collection = 'groups';
protected static $unguarded = true;
public function users()
{
return $this->belongsToMany('User', null, 'groups', 'users');
}
}
...@@ -8,9 +8,9 @@ use Illuminate\Auth\Reminders\RemindableInterface; ...@@ -8,9 +8,9 @@ use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface { class User extends Eloquent implements UserInterface, RemindableInterface {
protected $collection = 'users'; protected $collection = 'users';
protected $dates = array('birthday'); protected $dates = array('birthday');
protected static $unguarded = true; protected static $unguarded = true;
public function books() public function books()
...@@ -27,12 +27,17 @@ class User extends Eloquent implements UserInterface, RemindableInterface { ...@@ -27,12 +27,17 @@ class User extends Eloquent implements UserInterface, RemindableInterface {
{ {
return $this->hasOne('Role'); return $this->hasOne('Role');
} }
public function clients() public function clients()
{ {
return $this->belongsToMany('Client'); return $this->belongsToMany('Client');
} }
public function groups()
{
return $this->belongsToMany('Group', null, 'users', 'groups');
}
/** /**
* Get the unique identifier for the user. * Get the unique identifier for the user.
* *
...@@ -62,4 +67,4 @@ class User extends Eloquent implements UserInterface, RemindableInterface { ...@@ -62,4 +67,4 @@ class User extends Eloquent implements UserInterface, RemindableInterface {
{ {
return $this->email; return $this->email;
} }
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment