Commit 5d3343b3 authored by Jens Segers's avatar Jens Segers

Adding morph, #126

parent b6250367
...@@ -34,6 +34,9 @@ ...@@ -34,6 +34,9 @@
</testsuite> </testsuite>
<testsuite name="relations"> <testsuite name="relations">
<directory>tests/RelationsTest.php</directory> <directory>tests/RelationsTest.php</directory>
</testsuite>
<testsuite name="mysqlrelations">
<directory>tests/RelationsTest.php</directory>
<directory>tests/MysqlRelationsTest.php</directory> <directory>tests/MysqlRelationsTest.php</directory>
</testsuite> </testsuite>
</testsuites> </testsuites>
......
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
use Illuminate\Database\Eloquent\Relations\HasOne; use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\HasMany; use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Jenssegers\Mongodb\Relations\BelongsTo; use Jenssegers\Mongodb\Relations\BelongsTo;
use Jenssegers\Mongodb\Relations\BelongsToMany; use Jenssegers\Mongodb\Relations\BelongsToMany;
use Jenssegers\Mongodb\Query\Builder as QueryBuilder; use Jenssegers\Mongodb\Query\Builder as QueryBuilder;
...@@ -33,6 +35,35 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model { ...@@ -33,6 +35,35 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
return new HasOne($instance->newQuery(), $this, $foreignKey, $localKey); return new HasOne($instance->newQuery(), $this, $foreignKey, $localKey);
} }
/**
* Define a polymorphic one-to-one relationship.
*
* @param string $related
* @param string $name
* @param string $type
* @param string $id
* @param string $localKey
* @return \Illuminate\Database\Eloquent\Relations\MorphOne
*/
public function morphOne($related, $name, $type = null, $id = null, $localKey = null)
{
// Check if it is a relation with an original model.
if (!is_subclass_of($related, 'Jenssegers\Mongodb\Model'))
{
return parent::morphOne($related, $name, $type, $id, $localKey );
}
$instance = new $related;
list($type, $id) = $this->getMorphs($name, $type, $id);
$table = $instance->getTable();
$localKey = $localKey ?: $this->getKeyName();
return new MorphOne($instance->newQuery(), $this, $type, $id, $localKey);
}
/** /**
* Define a one-to-many relationship. * Define a one-to-many relationship.
* *
...@@ -58,6 +89,38 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model { ...@@ -58,6 +89,38 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
return new HasMany($instance->newQuery(), $this, $foreignKey, $localKey); return new HasMany($instance->newQuery(), $this, $foreignKey, $localKey);
} }
/**
* Define a polymorphic one-to-many relationship.
*
* @param string $related
* @param string $name
* @param string $type
* @param string $id
* @param string $localKey
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function morphMany($related, $name, $type = null, $id = null, $localKey = null)
{
// Check if it is a relation with an original model.
if (!is_subclass_of($related, 'Jenssegers\Mongodb\Model'))
{
return parent::morphMany($related, $name, $type, $id, $localKey);
}
$instance = new $related;
// Here we will gather up the morph type and ID for the relationship so that we
// can properly query the intermediate table of a relation. Finally, we will
// get the table and create the relationship instances for the developers.
list($type, $id) = $this->getMorphs($name, $type, $id);
$table = $instance->getTable();
$localKey = $localKey ?: $this->getKeyName();
return new MorphMany($instance->newQuery(), $this, $type, $id, $localKey);
}
/** /**
* Define an inverse one-to-one or many relationship. * Define an inverse one-to-one or many relationship.
* *
......
...@@ -13,6 +13,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -13,6 +13,7 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
Role::truncate(); Role::truncate();
Client::truncate(); Client::truncate();
Group::truncate(); Group::truncate();
Photo::truncate();
} }
public function testHasMany() public function testHasMany()
...@@ -259,4 +260,23 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -259,4 +260,23 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$this->assertEquals($group->_id, $user->groups()->first()->_id); $this->assertEquals($group->_id, $user->groups()->first()->_id);
$this->assertEquals($user->_id, $group->users()->first()->_id); $this->assertEquals($user->_id, $group->users()->first()->_id);
} }
public function testMorph()
{
$user = User::create(array('name' => 'John Doe'));
$client = Client::create(array('name' => 'Jane Doe'));
$photo = Photo::create(array('url' => 'http://graph.facebook.com/john.doe/picture'));
$user->photos()->save($photo);
$this->assertEquals(1, $user->photos->count());
$this->assertEquals($photo->id, $user->photos->first()->id);
$photo = Photo::create(array('url' => 'http://graph.facebook.com/john.doe/picture'));
$client->photos()->save($photo);
$this->assertEquals(1, $client->photos->count());
$this->assertEquals($photo->id, $client->photos->first()->id);
$photo = Photo::first();
$this->assertEquals($photo->imageable->name, $user->name);
}
} }
...@@ -11,4 +11,9 @@ class Client extends Eloquent { ...@@ -11,4 +11,9 @@ class Client extends Eloquent {
{ {
return $this->belongsToMany('User'); return $this->belongsToMany('User');
} }
public function photos()
{
return $this->morphMany('Photo', 'imageable');
}
} }
<?php
use Jenssegers\Mongodb\Model as Eloquent;
class Photo extends Eloquent {
protected $collection = 'photos';
protected static $unguarded = true;
public function imageable()
{
return $this->morphTo();
}
}
...@@ -46,6 +46,11 @@ class User extends Eloquent implements UserInterface, RemindableInterface { ...@@ -46,6 +46,11 @@ class User extends Eloquent implements UserInterface, RemindableInterface {
return $this->belongsToMany('Group', null, 'users', 'groups'); return $this->belongsToMany('Group', null, 'users', 'groups');
} }
public function photos()
{
return $this->morphMany('Photo', 'imageable');
}
/** /**
* Get the unique identifier for the user. * 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