Skip to content

Commit 5d3343b

Browse files
committed
Adding morph, #126
1 parent b625036 commit 5d3343b

File tree

6 files changed

+111
-0
lines changed

6 files changed

+111
-0
lines changed

phpunit.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@
3434
</testsuite>
3535
<testsuite name="relations">
3636
<directory>tests/RelationsTest.php</directory>
37+
</testsuite>
38+
<testsuite name="mysqlrelations">
39+
<directory>tests/RelationsTest.php</directory>
3740
<directory>tests/MysqlRelationsTest.php</directory>
3841
</testsuite>
3942
</testsuites>

src/Jenssegers/Eloquent/Model.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
use Illuminate\Database\Eloquent\Relations\HasOne;
44
use Illuminate\Database\Eloquent\Relations\HasMany;
5+
use Illuminate\Database\Eloquent\Relations\MorphOne;
6+
use Illuminate\Database\Eloquent\Relations\MorphMany;
57
use Jenssegers\Mongodb\Relations\BelongsTo;
68
use Jenssegers\Mongodb\Relations\BelongsToMany;
79
use Jenssegers\Mongodb\Query\Builder as QueryBuilder;
@@ -33,6 +35,35 @@ public function hasOne($related, $foreignKey = null, $localKey = null)
3335
return new HasOne($instance->newQuery(), $this, $foreignKey, $localKey);
3436
}
3537

38+
/**
39+
* Define a polymorphic one-to-one relationship.
40+
*
41+
* @param string $related
42+
* @param string $name
43+
* @param string $type
44+
* @param string $id
45+
* @param string $localKey
46+
* @return \Illuminate\Database\Eloquent\Relations\MorphOne
47+
*/
48+
public function morphOne($related, $name, $type = null, $id = null, $localKey = null)
49+
{
50+
// Check if it is a relation with an original model.
51+
if (!is_subclass_of($related, 'Jenssegers\Mongodb\Model'))
52+
{
53+
return parent::morphOne($related, $name, $type, $id, $localKey );
54+
}
55+
56+
$instance = new $related;
57+
58+
list($type, $id) = $this->getMorphs($name, $type, $id);
59+
60+
$table = $instance->getTable();
61+
62+
$localKey = $localKey ?: $this->getKeyName();
63+
64+
return new MorphOne($instance->newQuery(), $this, $type, $id, $localKey);
65+
}
66+
3667
/**
3768
* Define a one-to-many relationship.
3869
*
@@ -58,6 +89,38 @@ public function hasMany($related, $foreignKey = null, $localKey = null)
5889
return new HasMany($instance->newQuery(), $this, $foreignKey, $localKey);
5990
}
6091

92+
/**
93+
* Define a polymorphic one-to-many relationship.
94+
*
95+
* @param string $related
96+
* @param string $name
97+
* @param string $type
98+
* @param string $id
99+
* @param string $localKey
100+
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
101+
*/
102+
public function morphMany($related, $name, $type = null, $id = null, $localKey = null)
103+
{
104+
// Check if it is a relation with an original model.
105+
if (!is_subclass_of($related, 'Jenssegers\Mongodb\Model'))
106+
{
107+
return parent::morphMany($related, $name, $type, $id, $localKey);
108+
}
109+
110+
$instance = new $related;
111+
112+
// Here we will gather up the morph type and ID for the relationship so that we
113+
// can properly query the intermediate table of a relation. Finally, we will
114+
// get the table and create the relationship instances for the developers.
115+
list($type, $id) = $this->getMorphs($name, $type, $id);
116+
117+
$table = $instance->getTable();
118+
119+
$localKey = $localKey ?: $this->getKeyName();
120+
121+
return new MorphMany($instance->newQuery(), $this, $type, $id, $localKey);
122+
}
123+
61124
/**
62125
* Define an inverse one-to-one or many relationship.
63126
*

tests/RelationsTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public function tearDown()
1313
Role::truncate();
1414
Client::truncate();
1515
Group::truncate();
16+
Photo::truncate();
1617
}
1718

1819
public function testHasMany()
@@ -259,4 +260,23 @@ public function testBelongsToManyCustom()
259260
$this->assertEquals($group->_id, $user->groups()->first()->_id);
260261
$this->assertEquals($user->_id, $group->users()->first()->_id);
261262
}
263+
264+
public function testMorph()
265+
{
266+
$user = User::create(array('name' => 'John Doe'));
267+
$client = Client::create(array('name' => 'Jane Doe'));
268+
269+
$photo = Photo::create(array('url' => 'http://graph.facebook.com/john.doe/picture'));
270+
$user->photos()->save($photo);
271+
$this->assertEquals(1, $user->photos->count());
272+
$this->assertEquals($photo->id, $user->photos->first()->id);
273+
274+
$photo = Photo::create(array('url' => 'http://graph.facebook.com/john.doe/picture'));
275+
$client->photos()->save($photo);
276+
$this->assertEquals(1, $client->photos->count());
277+
$this->assertEquals($photo->id, $client->photos->first()->id);
278+
279+
$photo = Photo::first();
280+
$this->assertEquals($photo->imageable->name, $user->name);
281+
}
262282
}

tests/models/Client.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,9 @@ public function users()
1111
{
1212
return $this->belongsToMany('User');
1313
}
14+
15+
public function photos()
16+
{
17+
return $this->morphMany('Photo', 'imageable');
18+
}
1419
}

tests/models/Photo.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
use Jenssegers\Mongodb\Model as Eloquent;
4+
5+
class Photo extends Eloquent {
6+
7+
protected $collection = 'photos';
8+
protected static $unguarded = true;
9+
10+
public function imageable()
11+
{
12+
return $this->morphTo();
13+
}
14+
15+
}

tests/models/User.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ public function groups()
4646
return $this->belongsToMany('Group', null, 'users', 'groups');
4747
}
4848

49+
public function photos()
50+
{
51+
return $this->morphMany('Photo', 'imageable');
52+
}
53+
4954
/**
5055
* Get the unique identifier for the user.
5156
*

0 commit comments

Comments
 (0)