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
d78e126c
Commit
d78e126c
authored
Apr 28, 2014
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow execution of collection methods on embedded relations
parent
f6e43f54
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
453 additions
and
422 deletions
+453
-422
phpunit.xml
phpunit.xml
+1
-0
EmbedsMany.php
src/Jenssegers/Mongodb/Relations/EmbedsMany.php
+25
-26
EmbeddedRelationsTest.php
tests/EmbeddedRelationsTest.php
+427
-0
RelationsTest.php
tests/RelationsTest.php
+0
-396
No files found.
phpunit.xml
View file @
d78e126c
...
...
@@ -34,6 +34,7 @@
</testsuite>
<testsuite
name=
"relations"
>
<directory>
tests/RelationsTest.php
</directory>
<directory>
tests/EmbeddedRelationsTest.php
</directory>
</testsuite>
<testsuite
name=
"mysqlrelations"
>
<directory>
tests/RelationsTest.php
</directory>
...
...
src/Jenssegers/Mongodb/Relations/EmbedsMany.php
View file @
d78e126c
...
...
@@ -19,36 +19,17 @@ class EmbedsMany extends EmbedsOneOrMany {
}
/**
*
Find an embedded model by its primary key
.
*
Simulate order by method
.
*
* @param mixed $id
* @return \Illuminate\Database\Eloquent\Collection
* @param string $column
* @param string $direction
* @return Illuminate\Database\Eloquent\Collection
*/
public
function
find
(
$id
)
public
function
orderBy
(
$column
,
$direction
=
'asc'
)
{
if
(
$id
instanceof
Model
)
$id
=
$id
->
getKey
();
$primaryKey
=
$this
->
related
->
getKeyName
();
$descending
=
strtolower
(
$direction
)
==
'desc'
;
// Traverse all embedded records and find the first record
// that matches the given primary key.
$record
=
array_first
(
$this
->
getEmbedded
(),
function
(
$itemKey
,
$record
)
use
(
$primaryKey
,
$id
)
{
return
$record
[
$primaryKey
]
==
$id
;
});
return
$record
?
$this
->
toModel
(
$record
)
:
null
;
}
/**
* Check if a model is already embedded.
*
* @param mixed $key
* @return bool
*/
public
function
contains
(
$key
)
{
return
!
is_null
(
$this
->
find
(
$key
));
return
$this
->
getResults
()
->
sortBy
(
$column
,
SORT_REGULAR
,
$descending
);
}
/**
...
...
@@ -314,4 +295,22 @@ class EmbedsMany extends EmbedsOneOrMany {
return
parent
::
setEmbedded
(
array_values
(
$models
));
}
/**
* Handle dynamic method calls to the relationship.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public
function
__call
(
$method
,
$parameters
)
{
// Collection methods
if
(
method_exists
(
'Illuminate\Database\Eloquent\Collection'
,
$method
))
{
return
call_user_func_array
(
array
(
$this
->
getResults
(),
$method
),
$parameters
);
}
return
parent
::
__call
(
$method
,
$parameters
);
}
}
tests/EmbeddedRelationsTest.php
0 → 100644
View file @
d78e126c
This diff is collapsed.
Click to expand it.
tests/RelationsTest.php
View file @
d78e126c
This diff is collapsed.
Click to expand it.
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