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
14db4cbb
Commit
14db4cbb
authored
Aug 09, 2014
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding more collection operations for #239
parent
415ed78e
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
100 additions
and
35 deletions
+100
-35
Collection.php
src/Jenssegers/Mongodb/Eloquent/Collection.php
+75
-0
EmbedsMany.php
src/Jenssegers/Mongodb/Relations/EmbedsMany.php
+3
-18
EmbedsOne.php
src/Jenssegers/Mongodb/Relations/EmbedsOne.php
+2
-3
EmbedsOneOrMany.php
src/Jenssegers/Mongodb/Relations/EmbedsOneOrMany.php
+7
-6
EmbeddedRelationsTest.php
tests/EmbeddedRelationsTest.php
+13
-8
No files found.
src/Jenssegers/Mongodb/Eloquent/Collection.php
0 → 100644
View file @
14db4cbb
<?php
namespace
Jenssegers\Mongodb\Eloquent
;
use
Illuminate\Database\Eloquent\Collection
as
BaseCollection
;
class
Collection
extends
BaseCollection
{
/**
* Simulate a basic where clause on the ollection.
*
* @param string $key
* @param string $operator
* @param mixed $value
* @param string $boolean
* @return $this
*/
public
function
where
(
$key
,
$operator
=
null
,
$value
=
null
)
{
// Here we will make some assumptions about the operator. If only 2 values are
// passed to the method, we will assume that the operator is an equals sign
// and keep going.
if
(
func_num_args
()
==
2
)
{
list
(
$value
,
$operator
)
=
array
(
$operator
,
'='
);
}
return
$this
->
filter
(
function
(
$item
)
use
(
$key
,
$operator
,
$value
)
{
$actual
=
$item
->
{
$key
};
switch
(
$operator
)
{
case
'<>'
:
case
'!='
:
return
$actual
!=
$value
;
break
;
case
'>'
:
return
$actual
>
$value
;
break
;
case
'<'
:
return
$actual
<
$value
;
break
;
case
'>='
:
return
$actual
>=
$value
;
break
;
case
'between'
:
return
$actual
>=
$value
[
0
]
and
$actual
<=
$value
[
1
];
break
;
case
'='
:
default
:
return
$actual
==
$value
;
break
;
}
});
}
/**
* Simulate order by.
*
* @param string $key
* @param string $direction
* @return Illuminate\Database\Eloquent\Collection
*/
public
function
orderBy
(
$key
,
$direction
=
'asc'
)
{
$descending
=
strtolower
(
$direction
)
==
'desc'
;
return
$this
->
sortBy
(
$key
,
SORT_REGULAR
,
$descending
);
}
}
src/Jenssegers/Mongodb/Relations/EmbedsMany.php
View file @
14db4cbb
<?php
namespace
Jenssegers\Mongodb\Relations
;
<?php
namespace
Jenssegers\Mongodb\Relations
;
use
MongoId
;
use
Illuminate\Database\Eloquent\Model
;
use
Illuminate\Database\Eloquent\Model
;
use
Illuminate\Database\Eloquent\Builder
;
use
Illuminate\Database\Eloquent\Builder
;
use
Illuminate\Database\Eloquent\Relations\Relation
;
use
Illuminate\Database\Eloquent\Relations\Relation
;
use
Illuminate\Database\Eloquent\Collection
;
use
MongoId
;
class
EmbedsMany
extends
EmbedsOneOrMany
{
class
EmbedsMany
extends
EmbedsOneOrMany
{
/**
/**
* Get the results of the relationship.
* Get the results of the relationship.
*
*
* @return
Illuminate\Database
\Eloquent\Collection
* @return
Jenssegers\Mongodb
\Eloquent\Collection
*/
*/
public
function
getResults
()
public
function
getResults
()
{
{
...
@@ -290,20 +289,6 @@ class EmbedsMany extends EmbedsOneOrMany {
...
@@ -290,20 +289,6 @@ class EmbedsMany extends EmbedsOneOrMany {
return
parent
::
setEmbedded
(
array_values
(
$models
));
return
parent
::
setEmbedded
(
array_values
(
$models
));
}
}
/**
* Simulate order by method.
*
* @param string $column
* @param string $direction
* @return Illuminate\Database\Eloquent\Collection
*/
public
function
orderBy
(
$column
,
$direction
=
'asc'
)
{
$descending
=
strtolower
(
$direction
)
==
'desc'
;
return
$this
->
getResults
()
->
sortBy
(
$column
,
SORT_REGULAR
,
$descending
);
}
/**
/**
* Handle dynamic method calls to the relationship.
* Handle dynamic method calls to the relationship.
*
*
...
@@ -314,7 +299,7 @@ class EmbedsMany extends EmbedsOneOrMany {
...
@@ -314,7 +299,7 @@ class EmbedsMany extends EmbedsOneOrMany {
public
function
__call
(
$method
,
$parameters
)
public
function
__call
(
$method
,
$parameters
)
{
{
// Collection methods
// Collection methods
if
(
method_exists
(
'
Illuminate\Database
\Eloquent\Collection'
,
$method
))
if
(
method_exists
(
'
Jenssegers\Mongodb
\Eloquent\Collection'
,
$method
))
{
{
return
call_user_func_array
(
array
(
$this
->
getResults
(),
$method
),
$parameters
);
return
call_user_func_array
(
array
(
$this
->
getResults
(),
$method
),
$parameters
);
}
}
...
...
src/Jenssegers/Mongodb/Relations/EmbedsOne.php
View file @
14db4cbb
<?php
namespace
Jenssegers\Mongodb\Relations
;
<?php
namespace
Jenssegers\Mongodb\Relations
;
use
MongoId
;
use
Illuminate\Database\Eloquent\Model
;
use
Illuminate\Database\Eloquent\Model
;
use
Illuminate\Database\Eloquent\Builder
;
use
Illuminate\Database\Eloquent\Builder
;
use
Illuminate\Database\Eloquent\Relations\Relation
;
use
Illuminate\Database\Eloquent\Relations\Relation
;
use
Illuminate\Database\Eloquent\Collection
;
use
MongoId
;
class
EmbedsOne
extends
EmbedsOneOrMany
{
class
EmbedsOne
extends
EmbedsOneOrMany
{
/**
/**
* Get the results of the relationship.
* Get the results of the relationship.
*
*
* @return
Illuminate\Database\Eloquent\Collection
* @return
\Illuminate\Database\Eloquent\Model
*/
*/
public
function
getResults
()
public
function
getResults
()
{
{
...
...
src/Jenssegers/Mongodb/Relations/EmbedsOneOrMany.php
View file @
14db4cbb
<?php
namespace
Jenssegers\Mongodb\Relations
;
<?php
namespace
Jenssegers\Mongodb\Relations
;
use
MongoId
;
use
Illuminate\Database\Eloquent\Model
;
use
Illuminate\Database\Eloquent\Model
;
use
Illuminate\Database\Eloquent\Builder
;
use
Illuminate\Database\Eloquent\Builder
;
use
Illuminate\Database\Eloquent\Relations\Relation
;
use
Illuminate\Database\Eloquent\Relations\Relation
;
use
Illuminate\Database\Eloquent\Collection
;
use
Illuminate\Database\Eloquent\Collection
as
BaseCollection
;
use
MongoId
;
use
Jenssegers\Mongodb\Eloquent\Collection
;
abstract
class
EmbedsOneOrMany
extends
Relation
{
abstract
class
EmbedsOneOrMany
extends
Relation
{
...
@@ -109,7 +110,7 @@ abstract class EmbedsOneOrMany extends Relation {
...
@@ -109,7 +110,7 @@ abstract class EmbedsOneOrMany extends Relation {
* @param string $relation
* @param string $relation
* @return array
* @return array
*/
*/
public
function
match
(
array
$models
,
Collection
$results
,
$relation
)
public
function
match
(
array
$models
,
Base
Collection
$results
,
$relation
)
{
{
foreach
(
$models
as
$model
)
foreach
(
$models
as
$model
)
{
{
...
@@ -126,7 +127,7 @@ abstract class EmbedsOneOrMany extends Relation {
...
@@ -126,7 +127,7 @@ abstract class EmbedsOneOrMany extends Relation {
/**
/**
* Shorthand to get the results of the relationship.
* Shorthand to get the results of the relationship.
*
*
* @return
Illuminate\Database
\Eloquent\Collection
* @return
Jenssegers\Mongodb
\Eloquent\Collection
*/
*/
public
function
get
()
public
function
get
()
{
{
...
@@ -278,7 +279,7 @@ abstract class EmbedsOneOrMany extends Relation {
...
@@ -278,7 +279,7 @@ abstract class EmbedsOneOrMany extends Relation {
* Convert an array of records to a Collection.
* Convert an array of records to a Collection.
*
*
* @param array $records
* @param array $records
* @return
Illuminate\Database
\Eloquent\Collection
* @return
Jenssegers\Mongodb
\Eloquent\Collection
*/
*/
protected
function
toCollection
(
array
$records
=
array
())
protected
function
toCollection
(
array
$records
=
array
())
{
{
...
@@ -294,7 +295,7 @@ abstract class EmbedsOneOrMany extends Relation {
...
@@ -294,7 +295,7 @@ abstract class EmbedsOneOrMany extends Relation {
$models
=
$this
->
eagerLoadRelations
(
$models
);
$models
=
$this
->
eagerLoadRelations
(
$models
);
}
}
return
$this
->
related
->
new
Collection
(
$models
);
return
new
Collection
(
$models
);
}
}
/**
/**
...
...
tests/EmbeddedRelationsTest.php
View file @
14db4cbb
...
@@ -416,14 +416,19 @@ class EmbeddedRelationsTest extends TestCase {
...
@@ -416,14 +416,19 @@ class EmbeddedRelationsTest extends TestCase {
public
function
testEmbedsManyCollectionMethods
()
public
function
testEmbedsManyCollectionMethods
()
{
{
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
$user
=
User
::
create
(
array
(
'name'
=>
'John Doe'
));
$user
->
addresses
()
->
save
(
new
Address
(
array
(
'city'
=>
'New York'
)));
$user
->
addresses
()
->
save
(
new
Address
(
array
(
'city'
=>
'Paris'
,
'country'
=>
'France'
)));
$user
->
addresses
()
->
save
(
new
Address
(
array
(
'city'
=>
'Paris'
)));
$user
->
addresses
()
->
save
(
new
Address
(
array
(
'city'
=>
'Bruges'
,
'country'
=>
'Belgium'
)));
$user
->
addresses
()
->
save
(
new
Address
(
array
(
'city'
=>
'Brussels'
)));
$user
->
addresses
()
->
save
(
new
Address
(
array
(
'city'
=>
'Brussels'
,
'country'
=>
'Belgium'
)));
$user
->
addresses
()
->
save
(
new
Address
(
array
(
'city'
=>
'Ghent'
,
'country'
=>
'Belgium'
)));
$this
->
assertEquals
(
array
(
'New York'
,
'Paris'
,
'Brussels'
),
$user
->
addresses
()
->
lists
(
'city'
));
$this
->
assertEquals
(
array
(
'Brussels'
,
'New York'
,
'Paris'
),
$user
->
addresses
()
->
sortBy
(
'city'
)
->
lists
(
'city'
));
$this
->
assertEquals
(
array
(
'Paris'
,
'Bruges'
,
'Brussels'
,
'Ghent'
),
$user
->
addresses
()
->
lists
(
'city'
));
$this
->
assertEquals
(
array
(
'Brussels'
,
'New York'
,
'Paris'
),
$user
->
addresses
()
->
orderBy
(
'city'
)
->
lists
(
'city'
));
$this
->
assertEquals
(
array
(
'Bruges'
,
'Brussels'
,
'Ghent'
,
'Paris'
),
$user
->
addresses
()
->
sortBy
(
'city'
)
->
lists
(
'city'
));
$this
->
assertEquals
(
array
(
'Paris'
,
'New York'
,
'Brussels'
),
$user
->
addresses
()
->
orderBy
(
'city'
,
'desc'
)
->
lists
(
'city'
));
$this
->
assertEquals
(
array
(
'Bruges'
,
'Brussels'
,
'Ghent'
,
'Paris'
),
$user
->
addresses
()
->
orderBy
(
'city'
)
->
lists
(
'city'
));
$this
->
assertEquals
(
array
(
'Paris'
,
'Ghent'
,
'Brussels'
,
'Bruges'
),
$user
->
addresses
()
->
orderBy
(
'city'
,
'desc'
)
->
lists
(
'city'
));
$this
->
assertEquals
(
array
(),
$user
->
addresses
()
->
where
(
'city'
,
'New York'
)
->
lists
(
'city'
));
$this
->
assertEquals
(
array
(
'Bruges'
,
'Brussels'
,
'Ghent'
),
$user
->
addresses
()
->
where
(
'country'
,
'Belgium'
)
->
lists
(
'city'
));
$this
->
assertEquals
(
array
(
'Ghent'
,
'Brussels'
,
'Bruges'
),
$user
->
addresses
()
->
where
(
'country'
,
'Belgium'
)
->
orderBy
(
'city'
,
'desc'
)
->
lists
(
'city'
));
}
}
public
function
testEmbedsOne
()
public
function
testEmbedsOne
()
...
...
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