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

Merge pull request #70 from DyeH/master

Many to Many relationships
parents 4d005877 0eed8964
...@@ -4,4 +4,5 @@ phpunit.phar ...@@ -4,4 +4,5 @@ phpunit.phar
composer.phar composer.phar
composer.lock composer.lock
*.sublime-project *.sublime-project
*.sublime-workspace *.sublime-workspace
\ No newline at end of file *.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.
* *
...@@ -274,4 +312,4 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model { ...@@ -274,4 +312,4 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
return parent::__call($method, $parameters); return parent::__call($method, $parameters);
} }
} }
\ No newline at end of file
This diff is collapsed.
...@@ -312,11 +312,14 @@ class ModelTest extends PHPUnit_Framework_TestCase { ...@@ -312,11 +312,14 @@ 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);
......
This diff is collapsed.
<?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
...@@ -7,13 +7,13 @@ use Illuminate\Auth\Reminders\RemindableInterface; ...@@ -7,13 +7,13 @@ 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 static $unguarded = true;
protected $dates = array('birthday'); public function books()
protected static $unguarded = true;
public function books()
{ {
return $this->hasMany('Book', 'author_id'); return $this->hasMany('Book', 'author_id');
} }
...@@ -27,6 +27,11 @@ class User extends Eloquent implements UserInterface, RemindableInterface { ...@@ -27,6 +27,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