Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
L
laravel-mongodb
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
laravel-mongodb
Commits
5d3343b3
Commit
5d3343b3
authored
Feb 13, 2014
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding morph, #126
parent
b6250367
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
111 additions
and
0 deletions
+111
-0
phpunit.xml
phpunit.xml
+3
-0
Model.php
src/Jenssegers/Eloquent/Model.php
+63
-0
RelationsTest.php
tests/RelationsTest.php
+20
-0
Client.php
tests/models/Client.php
+5
-0
Photo.php
tests/models/Photo.php
+15
-0
User.php
tests/models/User.php
+5
-0
No files found.
phpunit.xml
View file @
5d3343b3
...
@@ -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>
...
...
src/Jenssegers/Eloquent/Model.php
View file @
5d3343b3
...
@@ -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.
*
*
...
...
tests/RelationsTest.php
View file @
5d3343b3
...
@@ -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
);
}
}
}
tests/models/Client.php
View file @
5d3343b3
...
@@ -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'
);
}
}
}
tests/models/Photo.php
0 → 100644
View file @
5d3343b3
<?php
use
Jenssegers\Mongodb\Model
as
Eloquent
;
class
Photo
extends
Eloquent
{
protected
$collection
=
'photos'
;
protected
static
$unguarded
=
true
;
public
function
imageable
()
{
return
$this
->
morphTo
();
}
}
tests/models/User.php
View file @
5d3343b3
...
@@ -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.
*
*
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment