1
- <?php namespace Laravel \Database \Eloquent ; use Laravel \Database ;
1
+ <?php namespace Laravel \Database \Eloquent ;
2
+
3
+ use Laravel \Database ;
4
+ use Laravel \Database \Eloquent \Relationships \Has_Many_And_Belongs_To ;
2
5
3
6
class Query {
4
7
@@ -54,7 +57,7 @@ public function __construct($model)
54
57
*/
55
58
public function first ($ columns = array ('* ' ))
56
59
{
57
- $ results = $ this ->hydrate ($ this ->model , $ this ->table ->take (1 )->get ($ columns, false ));
60
+ $ results = $ this ->hydrate ($ this ->model , $ this ->table ->take (1 )->get ($ columns ));
58
61
59
62
return (count ($ results ) > 0 ) ? head ($ results ) : null ;
60
63
}
@@ -63,12 +66,12 @@ public function first($columns = array('*'))
63
66
* Get all of the model results for the query.
64
67
*
65
68
* @param array $columns
66
- * @param bool $include
69
+ * @param bool $keyed
67
70
* @return array
68
71
*/
69
- public function get ($ columns = array ('* ' ), $ include = true )
72
+ public function get ($ columns = array ('* ' ), $ keyed = true )
70
73
{
71
- return $ this ->hydrate ($ this ->model , $ this ->table ->get ($ columns ), $ include );
74
+ return $ this ->hydrate ($ this ->model , $ this ->table ->get ($ columns ), $ keyed );
72
75
}
73
76
74
77
/**
@@ -97,9 +100,10 @@ public function paginate($per_page = null, $columns = array('*'))
97
100
*
98
101
* @param Model $model
99
102
* @param array $results
103
+ * @param bool $keyed
100
104
* @return array
101
105
*/
102
- public function hydrate ($ model , $ results , $ include = true )
106
+ public function hydrate ($ model , $ results , $ keyed = true )
103
107
{
104
108
$ class = get_class ($ model );
105
109
@@ -124,10 +128,20 @@ public function hydrate($model, $results, $include = true)
124
128
125
129
$ new ->original = $ new ->attributes ;
126
130
127
- $ models [$ result [$ this ->model ->key ()]] = $ new ;
131
+ // Typically, the resulting models are keyed by their primary key, but it
132
+ // may be useful to not do this in some circumstances such as when we
133
+ // are eager loading a *-to-* relationships which has duplicates.
134
+ if ($ keyed )
135
+ {
136
+ $ models [$ result [$ this ->model ->key ()]] = $ new ;
137
+ }
138
+ else
139
+ {
140
+ $ models [] = $ new ;
141
+ }
128
142
}
129
143
130
- if ($ include and count ($ results ) > 0 )
144
+ if (count ($ results ) > 0 )
131
145
{
132
146
foreach ($ this ->model_includes () as $ relationship => $ constraints )
133
147
{
@@ -183,12 +197,19 @@ protected function load(&$results, $relationship, $constraints)
183
197
$ query ->table ->where_nested ($ constraints );
184
198
}
185
199
186
- // Before matching the models, we will initialize the relationship
187
- // to either null for single-value relationships or an array for
188
- // the multi-value relationships as their baseline value.
189
200
$ query ->initialize ($ results , $ relationship );
190
201
191
- $ query ->match ($ relationship , $ results , $ query ->get ());
202
+ // If we're eager loading a many-to-many relationship we will disable
203
+ // the primary key indexing on the hydration since there could be
204
+ // roles shared across users and we don't want to overwrite.
205
+ if ( ! $ query instanceof Has_Many_And_Belongs_To)
206
+ {
207
+ $ query ->match ($ relationship , $ results , $ query ->get ());
208
+ }
209
+ else
210
+ {
211
+ $ query ->match ($ relationship , $ results , $ query ->get (array ('* ' ), false ));
212
+ }
192
213
}
193
214
194
215
/**
0 commit comments