@@ -194,24 +194,21 @@ private function addInverseRelations(array &$result): void
194194 return ;
195195 }
196196
197- foreach ($ result as $ i => $ relatedModel ) {
198- if ($ relatedModel instanceof ActiveRecordInterface) {
199- if (!isset ($ inverseRelation )) {
200- /** @var ActiveQuery $inverseRelation */
201- $ inverseRelation = $ relatedModel ->relationQuery ($ this ->inverseOf );
202- }
203- $ relatedModel ->populateRelation (
204- $ this ->inverseOf ,
205- $ inverseRelation ->multiple ? [$ this ->primaryModel ] : $ this ->primaryModel
206- );
207- } else {
208- if (!isset ($ inverseRelation )) {
209- /** @var ActiveQuery $inverseRelation */
210- $ inverseRelation = $ this ->getARInstance ()->relationQuery ($ this ->inverseOf );
211- }
197+ $ relatedModel = reset ($ result );
198+
199+ if ($ relatedModel instanceof ActiveRecordInterface) {
200+ $ inverseRelation = $ relatedModel ->relationQuery ($ this ->inverseOf );
201+ $ primaryModel = $ inverseRelation ->getMultiple () ? [$ this ->primaryModel ] : $ this ->primaryModel ;
202+
203+ foreach ($ result as $ relatedModel ) {
204+ $ relatedModel ->populateRelation ($ this ->inverseOf , $ primaryModel );
205+ }
206+ } else {
207+ $ inverseRelation = $ this ->getARInstance ()->relationQuery ($ this ->inverseOf );
208+ $ primaryModel = $ inverseRelation ->getMultiple () ? [$ this ->primaryModel ] : $ this ->primaryModel ;
212209
213- $ result[ $ i ][ $ this -> inverseOf ] = $ inverseRelation -> multiple
214- ? [$ this ->primaryModel ] : $ this -> primaryModel ;
210+ foreach ( $ result as & $ relatedModel ) {
211+ $ relatedModel [$ this ->inverseOf ] = $ primaryModel ;
215212 }
216213 }
217214 }
0 commit comments