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
Everything else works just like the original Eloquent model. Read more about the Eloquent on http://laravel.com/docs/eloquent
### 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`:
......@@ -268,8 +267,6 @@ Supported relations are:
- belongsTo
- belongsToMany
*The belongsToMany relation will not use a pivot "table", but will push id's to a **related_ids** attribute instead.*
Example:
use Jenssegers\Mongodb\Model as Eloquent;
......@@ -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
### Raw Expressions
......
......@@ -144,7 +144,7 @@ class BelongsToMany extends EloquentBelongsToMany {
*/
protected function createAttachRecords($ids, array $attributes)
{
$records = array();;
$records = array();
// 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
......@@ -170,6 +170,9 @@ class BelongsToMany extends EloquentBelongsToMany {
$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
// associations, otherwise all of the association ties will be broken.
// We'll return the numbers of affected rows when we do the deletes.
......@@ -185,11 +188,14 @@ class BelongsToMany extends EloquentBelongsToMany {
// 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
// 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);
}
// Remove the relation from the related model
$related->pull($this->foreignKey, $this->parent->getKey());
return count($ids);
}
......
......@@ -12,6 +12,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
Item::truncate();
Role::truncate();
Client::truncate();
Group::truncate();
}
public function testHasMany()
......@@ -126,17 +127,22 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$this->assertEquals('admin', $role->type);
}
public function testHasManyAndBelongsTo()
public function testBelongsToMany()
{
$user = User::create(array('name' => 'John Doe'));
// Add 2 clients
$user->clients()->save(new Client(array('name' => 'Pork Pies Ltd.')));
$user->clients()->create(array('name' => 'Buffet Bar Inc.'));
// Refetch
$user = User::with('clients')->find($user->_id);
$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');
$users = $user->getRelation('clients');
......@@ -148,15 +154,13 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$this->assertCount(1, $client->users);
// 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);
$this->assertInstanceOf('Client', $otherClient[0]);
$this->assertCount(1, $otherClient);
// Now attach an existing client to an existing user
// Get user and unattached client
$user = User::where('name', '=', 'Jane Doe')->first();
$client = Client::Where('name', '=', 'Buffet Bar Inc.')->first();
......@@ -167,6 +171,8 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
// 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(1, $user->clients);
$this->assertCount(1, $client->users);
// Attach the client to the user
$user->clients()->attach($client);
......@@ -178,9 +184,24 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
// Assert they are attached
$this->assertTrue(in_array($client->_id, $user->client_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')));
......@@ -190,8 +211,8 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
);
$moreClients = array(
Client::create(array('name' => 'Boloni Ltd.'))->_id,
Client::create(array('name' => 'Meatballs Inc.'))->_id
Client::create(array('name' => 'synced Boloni Ltd.'))->_id,
Client::create(array('name' => 'synced Meatballs Inc.'))->_id
);
// Sync multiple records
......@@ -205,11 +226,37 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
// Assert there are two client objects in the relationship
$this->assertCount(2, $user->clients);
// Add more clients
$user->clients()->sync($moreClients);
// Refetch
$user = User::with('clients')->find($user->_id);
// Assert there are now 4 client objects in the relationship
$this->assertCount(4, $user->clients);
// Assert there are now still 2 client objects in the relationship
$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);
}
}
<?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');
}
}
......@@ -33,6 +33,11 @@ class User extends Eloquent implements UserInterface, RemindableInterface {
return $this->belongsToMany('Client');
}
public function groups()
{
return $this->belongsToMany('Group', null, 'users', 'groups');
}
/**
* Get the unique identifier for the user.
*
......
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