Commit 12e7cb2a authored by Jens Segers's avatar Jens Segers

Merge pull request #70 from DyeH/master

Many to Many relationships
parents 4d005877 0eed8964
...@@ -5,3 +5,4 @@ composer.phar ...@@ -5,3 +5,4 @@ composer.phar
composer.lock composer.lock
*.sublime-project *.sublime-project
*.sublime-workspace *.sublime-workspace
*.project
...@@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany; ...@@ -7,6 +7,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
use Jenssegers\Mongodb\DatabaseManager as Resolver; use Jenssegers\Mongodb\DatabaseManager as Resolver;
use Jenssegers\Mongodb\Builder as QueryBuilder; use Jenssegers\Mongodb\Builder as QueryBuilder;
use Jenssegers\Mongodb\Relations\BelongsTo; use Jenssegers\Mongodb\Relations\BelongsTo;
use Jenssegers\Mongodb\Relations\BelongsToMany;
use Carbon\Carbon; use Carbon\Carbon;
use DateTime; use DateTime;
...@@ -199,6 +200,43 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model { ...@@ -199,6 +200,43 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
return new BelongsTo($query, $this, $foreignKey, $otherKey, $relation); return new BelongsTo($query, $this, $foreignKey, $otherKey, $relation);
} }
/**
* Define a many-to-many relationship.
*
* @param string $related
* @param string $table
* @param string $foreignKey
* @param string $otherKey
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function belongsToMany($related, $collection = null, $foreignKey = null, $otherKey = null)
{
$caller = $this->getBelongsToManyCaller();
// First, we'll need to determine the foreign key and "other key" for the
// relationship. Once we have determined the keys we'll make the query
// instances as well as the relationship instances we need for this.
$foreignKey = $foreignKey ?: $this->getForeignKey() . 's';
$instance = new $related;
$otherKey = $otherKey ?: $instance->getForeignKey() . 's';
// If no table name was provided, we can guess it by concatenating the two
// models using underscores in alphabetical order. The two model names
// are transformed to snake case from their default CamelCase also.
if (is_null($collection))
{
$collection = snake_case(str_plural(class_basename($related)));
}
// Now we're ready to create a new query builder for the related model and
// the relationship instances for the relation. The relations will set
// appropriate query constraint and entirely manages the hydrations.
$query = $instance->newQuery();
return new BelongsToMany($query, $this, $collection, $foreignKey, $otherKey, $caller['function']);
}
/** /**
* Get a new query builder instance for the connection. * Get a new query builder instance for the connection.
* *
......
This diff is collapsed.
...@@ -312,12 +312,15 @@ class ModelTest extends PHPUnit_Framework_TestCase { ...@@ -312,12 +312,15 @@ class ModelTest extends PHPUnit_Framework_TestCase {
public function testDates() public function testDates()
{ {
$user = User::create(array('name' => 'John Doe', 'birthday' => new DateTime('1980/1/1'))); $user = User::create(array('name' => 'John Doe', 'birthday' => new DateTime('1980/1/1')));
$this->assertInstanceOf('Carbon\Carbon', $user->birthday); $this->assertInstanceOf('Carbon\Carbon', $user->birthday);
$check = User::find($user->_id); $check = User::find($user->_id);
$this->assertInstanceOf('Carbon\Carbon', $check->birthday); $this->assertInstanceOf('Carbon\Carbon', $check->birthday);
$this->assertEquals($user->birthday, $check->birthday); $this->assertEquals($user->birthday, $check->birthday);
$user = User::where('birthday', '>', new DateTime('1975/1/1'))->first(); $user = User::where('birthday', '>', new DateTime('1975/1/1'))->first();
$this->assertEquals('John Doe', $user->name); $this->assertEquals('John Doe', $user->name);
} }
......
...@@ -11,6 +11,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -11,6 +11,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
Book::truncate(); Book::truncate();
Item::truncate(); Item::truncate();
Role::truncate(); Role::truncate();
Client::truncate();
} }
public function testHasMany() public function testHasMany()
...@@ -125,4 +126,90 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -125,4 +126,90 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$this->assertEquals('admin', $role->type); $this->assertEquals('admin', $role->type);
} }
public function testHasManyAndBelongsTo()
{
$user = User::create(array('name' => 'John Doe'));
$user->clients()->save(new Client(array('name' => 'Pork Pies Ltd.')));
$user->clients()->create(array('name' => 'Buffet Bar Inc.'));
$user = User::with('clients')->find($user->_id);
$client = Client::with('users')->first();
$clients = $client->getRelation('users');
$users = $user->getRelation('clients');
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $users);
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $clients);
$this->assertInstanceOf('Client', $users[0]);
$this->assertInstanceOf('User', $clients[0]);
$this->assertCount(2, $user->clients);
$this->assertCount(1, $client->users);
// Now create a new user to an existing client
$client->users()->create(array('name' => 'Jane Doe'));
$otherClient = User::where('name', '=', 'Jane Doe')->first()->clients()->get();
$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
$user = User::where('name', '=', 'Jane Doe')->first();
$client = Client::Where('name', '=', 'Buffet Bar Inc.')->first();
// Check the models are what they should be
$this->assertInstanceOf('Client', $client);
$this->assertInstanceOf('User', $user);
// Assert they are not attached
$this->assertFalse(in_array($client->_id, $user->client_ids));
$this->assertFalse(in_array($user->_id, $client->user_ids));
// Attach the client to the user
$user->clients()->attach($client);
// Get the new user model
$user = User::where('name', '=', 'Jane Doe')->first();
$client = Client::Where('name', '=', 'Buffet Bar Inc.')->first();
// Assert they are attached
$this->assertTrue(in_array($client->_id, $user->client_ids));
$this->assertTrue(in_array($user->_id, $client->user_ids));
}
public function testHasManyAndBelongsToAttachesExistingModels()
{
$user = User::create(array('name' => 'John Doe', 'client_ids' => array('1234523')));
$clients = array(
Client::create(array('name' => 'Pork Pies Ltd.'))->_id,
Client::create(array('name' => 'Buffet Bar Inc.'))->_id
);
$moreClients = array(
Client::create(array('name' => 'Boloni Ltd.'))->_id,
Client::create(array('name' => 'Meatballs Inc.'))->_id
);
// Sync multiple records
$user->clients()->sync($clients);
$user = User::with('clients')->find($user->_id);
// Assert non attached ID's are detached succesfully
$this->assertFalse(in_array('1234523', $user->client_ids));
// Assert there are two client objects in the relationship
$this->assertCount(2, $user->clients);
$user->clients()->sync($moreClients);
$user = User::with('clients')->find($user->_id);
// Assert there are now 4 client objects in the relationship
$this->assertCount(4, $user->clients);
}
} }
<?php
use Jenssegers\Mongodb\Model as Eloquent;
class Client extends Eloquent {
protected $collection = 'clients';
protected static $unguarded = true;
public function users()
{
return $this->belongsToMany('User');
}
}
\ No newline at end of file
...@@ -28,6 +28,11 @@ class User extends Eloquent implements UserInterface, RemindableInterface { ...@@ -28,6 +28,11 @@ class User extends Eloquent implements UserInterface, RemindableInterface {
return $this->hasOne('Role'); return $this->hasOne('Role');
} }
public function clients()
{
return $this->belongsToMany('Client');
}
/** /**
* Get the unique identifier for the user. * Get the unique identifier for the user.
* *
...@@ -57,5 +62,4 @@ class User extends Eloquent implements UserInterface, RemindableInterface { ...@@ -57,5 +62,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