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
a1d94104
Commit
a1d94104
authored
Mar 02, 2014
by
Jens Segers
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed eager loading, issue #138
parent
48ac180d
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
83 additions
and
65 deletions
+83
-65
Model.php
src/Jenssegers/Eloquent/Model.php
+0
-35
Model.php
src/Jenssegers/Mongodb/Model.php
+35
-5
EmbedsMany.php
src/Jenssegers/Mongodb/Relations/EmbedsMany.php
+48
-25
No files found.
src/Jenssegers/Eloquent/Model.php
View file @
a1d94104
...
...
@@ -7,7 +7,6 @@ use Illuminate\Database\Eloquent\Relations\MorphMany;
use
Illuminate\Database\Eloquent\Relations\Relation
;
use
Jenssegers\Mongodb\Relations\BelongsTo
;
use
Jenssegers\Mongodb\Relations\BelongsToMany
;
use
Jenssegers\Mongodb\Relations\EmbedsMany
;
use
Jenssegers\Mongodb\Query\Builder
as
QueryBuilder
;
abstract
class
Model
extends
\Illuminate\Database\Eloquent\Model
{
...
...
@@ -221,40 +220,6 @@ abstract class Model extends \Illuminate\Database\Eloquent\Model {
return
new
BelongsToMany
(
$query
,
$this
,
$collection
,
$foreignKey
,
$otherKey
,
$relation
);
}
/**
* Define an embedded one-to-many relationship.
*
* @param string $related
* @param string $collection
* @return \Illuminate\Database\Eloquent\Relations\EmbedsMany
*/
protected
function
embedsMany
(
$related
,
$localKey
=
null
,
$foreignKey
=
null
,
$relation
=
null
)
{
if
(
is_null
(
$localKey
))
{
$localKey
=
snake_case
(
str_plural
(
$related
))
.
'_ids'
;
}
if
(
is_null
(
$foreignKey
))
{
$foreignKey
=
snake_case
(
class_basename
(
$this
));
}
// If no relation name was given, we will use this debug backtrace to extract
// the calling method's name and use that as the relationship name as most
// of the time this will be what we desire to use for the relatinoships.
if
(
is_null
(
$relation
))
{
list
(,
$caller
)
=
debug_backtrace
(
false
);
$relation
=
$caller
[
'function'
];
}
$query
=
$this
->
newQuery
();
return
new
EmbedsMany
(
$query
,
$this
,
$localKey
,
$foreignKey
,
$relation
);
}
/**
* Get a new query builder instance for the connection.
*
...
...
src/Jenssegers/Mongodb/Model.php
View file @
a1d94104
<?php
namespace
Jenssegers\Mongodb
;
use
Illuminate\Database\Eloquent\Collection
;
use
Illuminate\Database\Eloquent\Relations\HasOne
;
use
Illuminate\Database\Eloquent\Relations\HasMany
;
use
Jenssegers\Mongodb\DatabaseManager
as
Resolver
;
use
Jenssegers\Mongodb\Eloquent\Builder
;
use
Jenssegers\Mongodb\Query\Builder
as
QueryBuilder
;
use
Jenssegers\Mongodb\Relations\BelongsTo
;
use
Jenssegers\Mongodb\Relations\BelongsToMany
;
use
Jenssegers\Mongodb\Relations\EmbedsMany
;
use
Carbon\Carbon
;
use
DateTime
;
...
...
@@ -52,6 +48,40 @@ abstract class Model extends \Jenssegers\Eloquent\Model {
if
(
array_key_exists
(
$this
->
getKeyName
(),
$this
->
attributes
))
return
$this
->
attributes
[
$this
->
getKeyName
()];
}
/**
* Define an embedded one-to-many relationship.
*
* @param string $related
* @param string $collection
* @return \Illuminate\Database\Eloquent\Relations\EmbedsMany
*/
protected
function
embedsMany
(
$related
,
$localKey
=
null
,
$foreignKey
=
null
,
$relation
=
null
)
{
if
(
is_null
(
$localKey
))
{
$localKey
=
snake_case
(
str_plural
(
$related
))
.
'_ids'
;
}
if
(
is_null
(
$foreignKey
))
{
$foreignKey
=
snake_case
(
class_basename
(
$this
));
}
// If no relation name was given, we will use this debug backtrace to extract
// the calling method's name and use that as the relationship name as most
// of the time this will be what we desire to use for the relatinoships.
if
(
is_null
(
$relation
))
{
list
(,
$caller
)
=
debug_backtrace
(
false
);
$relation
=
$caller
[
'function'
];
}
$query
=
$this
->
newQuery
();
return
new
EmbedsMany
(
$query
,
$this
,
$localKey
,
$foreignKey
,
$relation
);
}
/**
* Convert a DateTime to a storable MongoDate object.
*
...
...
src/Jenssegers/Mongodb/Relations/EmbedsMany.php
View file @
a1d94104
...
...
@@ -98,6 +98,18 @@ class EmbedsMany extends Relation {
*/
public
function
match
(
array
$models
,
Collection
$results
,
$relation
)
{
foreach
(
$models
as
$model
)
{
// Get raw attributes to skip relations and accessors.
$attributes
=
$model
->
getAttributes
();
$results
=
isset
(
$attributes
[
$this
->
localKey
])
?
$attributes
[
$this
->
localKey
]
:
array
();
$collection
=
$this
->
toCollection
(
$results
);
$model
->
setRelation
(
$relation
,
$collection
);
}
return
$models
;
}
...
...
@@ -109,22 +121,9 @@ class EmbedsMany extends Relation {
public
function
getResults
()
{
// Get embedded documents.
$results
=
$this
->
getEmbedded
();
$models
=
array
();
$results
=
$this
->
getEmbeddedRecords
();
// Wrap documents in model objects.
foreach
(
$results
as
$result
)
{
$model
=
$this
->
related
->
newFromBuilder
(
$result
);
// Attatch the parent relation to the embedded model.
$model
->
setRelation
(
$this
->
foreignKey
,
$this
->
parent
);
$models
[]
=
$model
;
}
return
$this
->
related
->
newCollection
(
$models
);
return
$this
->
toCollection
(
$results
);
}
/**
...
...
@@ -187,12 +186,12 @@ class EmbedsMany extends Relation {
$result
=
$this
->
query
->
push
(
$this
->
localKey
,
$model
->
getAttributes
(),
true
);
// Get existing embedded documents.
$documents
=
$this
->
getEmbedded
();
$documents
=
$this
->
getEmbedded
Records
();
// Add the document to the parent model.
$documents
[]
=
$model
->
getAttributes
();
$this
->
setEmbedded
(
$documents
);
$this
->
setEmbedded
Records
(
$documents
);
return
$result
?
$model
:
false
;
}
...
...
@@ -221,7 +220,7 @@ class EmbedsMany extends Relation {
->
update
(
array
(
$this
->
localKey
.
'.$'
=>
$model
->
getAttributes
()));
// Get existing embedded documents.
$documents
=
$this
->
getEmbedded
();
$documents
=
$this
->
getEmbedded
Records
();
$primaryKey
=
$this
->
related
->
getKeyName
();
...
...
@@ -237,7 +236,7 @@ class EmbedsMany extends Relation {
}
}
$this
->
setEmbedded
(
$documents
);
$this
->
setEmbedded
Records
(
$documents
);
return
$result
?
$model
:
false
;
}
...
...
@@ -325,7 +324,7 @@ class EmbedsMany extends Relation {
}
// Get existing embedded documents.
$documents
=
$this
->
getEmbedded
();
$documents
=
$this
->
getEmbedded
Records
();
// Remove the document from the parent model.
foreach
(
$documents
as
$i
=>
$document
)
...
...
@@ -336,7 +335,7 @@ class EmbedsMany extends Relation {
}
}
$this
->
setEmbedded
(
$documents
);
$this
->
setEmbedded
Records
(
$documents
);
return
$count
;
}
...
...
@@ -364,11 +363,35 @@ class EmbedsMany extends Relation {
}
/**
* Get the embedded documents array
* Convert an array of embedded documents to a Collection.
*
* @param array $results
* @return Illuminate\Database\Eloquent\Collection
*/
protected
function
toCollection
(
array
$results
=
array
())
{
$models
=
array
();
// Wrap documents in model objects.
foreach
(
$results
as
$result
)
{
$model
=
$this
->
related
->
newFromBuilder
(
$result
);
// Attatch the parent relation to the embedded model.
$model
->
setRelation
(
$this
->
foreignKey
,
$this
->
parent
);
$models
[]
=
$model
;
}
return
$this
->
related
->
newCollection
(
$models
);
}
/**
* Get the embedded documents array.
*
* @return array
*/
p
ublic
function
getEmbedded
()
p
rotected
function
getEmbeddedRecords
()
{
// Get raw attributes to skip relations and accessors.
$attributes
=
$this
->
parent
->
getAttributes
();
...
...
@@ -377,11 +400,11 @@ class EmbedsMany extends Relation {
}
/**
* Set the embedded documents array
* Set the embedded documents array
.
*
* @param array $models
*/
p
ublic
function
setEmbedded
(
array
$models
)
p
rotected
function
setEmbeddedRecords
(
array
$models
)
{
$attributes
=
$this
->
parent
->
getAttributes
();
...
...
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