Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
S
syncEnrollments
Project
Project
Details
Activity
Releases
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
Yeray Santana Hualde
syncEnrollments
Commits
94948cf6
Commit
94948cf6
authored
Apr 23, 2012
by
Taylor Otwell
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fix Eloquent eager loading matching.
parent
b65fa704
Changes
8
Show whitespace changes
Inline
Side-by-side
Showing
8 changed files
with
53 additions
and
42 deletions
+53
-42
query.php
laravel/database/eloquent/query.php
+5
-27
belongs_to.php
laravel/database/eloquent/relationships/belongs_to.php
+7
-2
has_many.php
laravel/database/eloquent/relationships/has_many.php
+7
-2
has_many_and_belongs_to.php
...tabase/eloquent/relationships/has_many_and_belongs_to.php
+7
-7
has_one.php
laravel/database/eloquent/relationships/has_one.php
+7
-2
has_one_or_many.php
laravel/database/eloquent/relationships/has_one_or_many.php
+1
-1
relationship.php
laravel/database/eloquent/relationships/relationship.php
+18
-0
error.php
laravel/error.php
+1
-1
No files found.
laravel/database/eloquent/query.php
View file @
94948cf6
...
@@ -66,12 +66,11 @@ class Query {
...
@@ -66,12 +66,11 @@ class Query {
* Get all of the model results for the query.
* Get all of the model results for the query.
*
*
* @param array $columns
* @param array $columns
* @param bool $keyed
* @return array
* @return array
*/
*/
public
function
get
(
$columns
=
array
(
'*'
)
,
$keyed
=
true
)
public
function
get
(
$columns
=
array
(
'*'
))
{
{
return
$this
->
hydrate
(
$this
->
model
,
$this
->
table
->
get
(
$columns
)
,
$keyed
);
return
$this
->
hydrate
(
$this
->
model
,
$this
->
table
->
get
(
$columns
));
}
}
/**
/**
...
@@ -100,10 +99,9 @@ class Query {
...
@@ -100,10 +99,9 @@ class Query {
*
*
* @param Model $model
* @param Model $model
* @param array $results
* @param array $results
* @param bool $keyed
* @return array
* @return array
*/
*/
public
function
hydrate
(
$model
,
$results
,
$keyed
=
true
)
public
function
hydrate
(
$model
,
$results
)
{
{
$class
=
get_class
(
$model
);
$class
=
get_class
(
$model
);
...
@@ -128,18 +126,8 @@ class Query {
...
@@ -128,18 +126,8 @@ class Query {
$new
->
original
=
$new
->
attributes
;
$new
->
original
=
$new
->
attributes
;
// Typically, the resulting models are keyed by their primary key, but it
// may be useful to not do this in some circumstances such as when we
// are eager loading a *-to-* relationships which has duplicates.
if
(
$keyed
)
{
$models
[
$result
[
$this
->
model
->
key
()]]
=
$new
;
}
else
{
$models
[]
=
$new
;
$models
[]
=
$new
;
}
}
}
if
(
count
(
$results
)
>
0
)
if
(
count
(
$results
)
>
0
)
{
{
...
@@ -199,18 +187,8 @@ class Query {
...
@@ -199,18 +187,8 @@ class Query {
$query
->
initialize
(
$results
,
$relationship
);
$query
->
initialize
(
$results
,
$relationship
);
// If we're eager loading a many-to-many relationship we will disable
// the primary key indexing on the hydration since there could be
// roles shared across users and we don't want to overwrite.
if
(
!
$query
instanceof
Has_Many_And_Belongs_To
)
{
$query
->
match
(
$relationship
,
$results
,
$query
->
get
());
$query
->
match
(
$relationship
,
$results
,
$query
->
get
());
}
}
else
{
$query
->
match
(
$relationship
,
$results
,
$query
->
get
(
array
(
'*'
),
false
));
}
}
/**
/**
* Gather the nested includes for a given relationship.
* Gather the nested includes for a given relationship.
...
...
laravel/database/eloquent/relationships/belongs_to.php
View file @
94948cf6
...
@@ -87,9 +87,14 @@ class Belongs_To extends Relationship {
...
@@ -87,9 +87,14 @@ class Belongs_To extends Relationship {
foreach
(
$children
as
&
$child
)
foreach
(
$children
as
&
$child
)
{
{
if
(
array_key_exists
(
$child
->
$foreign
,
$parents
)
)
$parent
=
array_first
(
$parents
,
function
(
$k
,
$v
)
use
(
$child
,
$foreign
)
{
{
$child
->
relationships
[
$relationship
]
=
$parents
[
$child
->
$foreign
];
return
$v
->
get_key
()
==
$child
->
$foreign
;
});
if
(
!
is_null
(
$parent
))
{
$child
->
relationships
[
$relationship
]
=
$parent
;
}
}
}
}
}
}
...
...
laravel/database/eloquent/relationships/has_many.php
View file @
94948cf6
...
@@ -91,9 +91,14 @@ class Has_Many extends Has_One_Or_Many {
...
@@ -91,9 +91,14 @@ class Has_Many extends Has_One_Or_Many {
{
{
$foreign
=
$this
->
foreign_key
();
$foreign
=
$this
->
foreign_key
();
foreach
(
$children
as
$key
=>
$child
)
foreach
(
$parents
as
&
$parent
)
{
$matching
=
array_filter
(
$children
,
function
(
$v
)
use
(
$parent
,
$foreign
)
{
{
$parents
[
$child
->
$foreign
]
->
relationships
[
$relationship
][
$child
->
get_key
()]
=
$child
;
return
$v
->
$foreign
==
$parent
->
get_key
();
});
$parent
->
relationships
[
$relationship
]
=
$matching
;
}
}
}
}
...
...
laravel/database/eloquent/relationships/has_many_and_belongs_to.php
View file @
94948cf6
...
@@ -311,7 +311,7 @@ class Has_Many_And_Belongs_To extends Relationship {
...
@@ -311,7 +311,7 @@ class Has_Many_And_Belongs_To extends Relationship {
*/
*/
public
function
eagerly_constrain
(
$results
)
public
function
eagerly_constrain
(
$results
)
{
{
$this
->
table
->
where_in
(
$this
->
joining
.
'.'
.
$this
->
foreign_key
(),
array_
keys
(
$results
));
$this
->
table
->
where_in
(
$this
->
joining
.
'.'
.
$this
->
foreign_key
(),
$this
->
keys
(
$results
));
}
}
/**
/**
...
@@ -325,14 +325,14 @@ class Has_Many_And_Belongs_To extends Relationship {
...
@@ -325,14 +325,14 @@ class Has_Many_And_Belongs_To extends Relationship {
{
{
$foreign
=
$this
->
foreign_key
();
$foreign
=
$this
->
foreign_key
();
// For each child we'll just get the parent that connects to the child and set the
foreach
(
$parents
as
&
$parent
)
// child model on the relationship array using the keys. Once we're done looping
{
// through the children all of the proper relations will be set.
$matching
=
array_filter
(
$children
,
function
(
$v
)
use
(
$parent
,
$foreign
)
foreach
(
$children
as
$key
=>
$child
)
{
{
$parent
=&
$parents
[
$child
->
pivot
->
$foreign
];
return
$v
->
pivot
->
$foreign
==
$parent
->
get_key
();
});
$parent
->
relationships
[
$relationship
]
[
$child
->
{
$child
->
key
()}]
=
$child
;
$parent
->
relationships
[
$relationship
]
=
$matching
;
}
}
}
}
...
...
laravel/database/eloquent/relationships/has_one.php
View file @
94948cf6
...
@@ -38,9 +38,14 @@ class Has_One extends Has_One_Or_Many {
...
@@ -38,9 +38,14 @@ class Has_One extends Has_One_Or_Many {
{
{
$foreign
=
$this
->
foreign_key
();
$foreign
=
$this
->
foreign_key
();
foreach
(
$children
as
$key
=>
$child
)
foreach
(
$parents
as
&
$parent
)
{
$matching
=
array_first
(
$children
,
function
(
$k
,
$v
)
use
(
$parent
,
$foreign
)
{
{
$parents
[
$child
->
$foreign
]
->
relationships
[
$relationship
]
=
$child
;
return
$v
->
$foreign
==
$parent
->
get_key
();
});
$parent
->
relationships
[
$relationship
]
=
$matching
;
}
}
}
}
...
...
laravel/database/eloquent/relationships/has_one_or_many.php
View file @
94948cf6
...
@@ -53,7 +53,7 @@ class Has_One_Or_Many extends Relationship {
...
@@ -53,7 +53,7 @@ class Has_One_Or_Many extends Relationship {
*/
*/
public
function
eagerly_constrain
(
$results
)
public
function
eagerly_constrain
(
$results
)
{
{
$this
->
table
->
where_in
(
$this
->
foreign_key
(),
array_
keys
(
$results
));
$this
->
table
->
where_in
(
$this
->
foreign_key
(),
$this
->
keys
(
$results
));
}
}
}
}
\ No newline at end of file
laravel/database/eloquent/relationships/relationship.php
View file @
94948cf6
...
@@ -101,4 +101,22 @@ abstract class Relationship extends Query {
...
@@ -101,4 +101,22 @@ abstract class Relationship extends Query {
return
static
::
foreign
(
$this
->
base
,
$this
->
foreign
);
return
static
::
foreign
(
$this
->
base
,
$this
->
foreign
);
}
}
/**
* Gather all the primary keys from a result set.
*
* @param array $results
* @return array
*/
public
function
keys
(
$results
)
{
$keys
=
array
();
foreach
(
$results
as
$result
)
{
$keys
[]
=
$result
->
get_key
();
}
return
array_unique
(
$keys
);
}
}
}
\ No newline at end of file
laravel/error.php
View file @
94948cf6
...
@@ -13,7 +13,7 @@ class Error {
...
@@ -13,7 +13,7 @@ class Error {
{
{
static
::
log
(
$exception
);
static
::
log
(
$exception
);
ob_get_level
()
and
ob_end_clean
();
//
ob_get_level() and ob_end_clean();
// If detailed errors are enabled, we'll just format the exception into
// If detailed errors are enabled, we'll just format the exception into
// a simple error message and display it on the screen. We don't use a
// a simple error message and display it on the screen. We don't use a
...
...
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