Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
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
PSDI Army
yii2
Commits
9ea4ef13
Commit
9ea4ef13
authored
Dec 16, 2013
by
Paul Klimov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
MongoDB Active Relation lazy load fixed.
parent
d3a181c6
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
39 additions
and
3 deletions
+39
-3
ActiveRelation.php
extensions/yii/mongodb/ActiveRelation.php
+32
-0
ActiveRelationTest.php
tests/unit/extensions/mongodb/ActiveRelationTest.php
+7
-3
No files found.
extensions/yii/mongodb/ActiveRelation.php
View file @
9ea4ef13
...
@@ -19,4 +19,35 @@ use yii\db\ActiveRelationTrait;
...
@@ -19,4 +19,35 @@ use yii\db\ActiveRelationTrait;
class
ActiveRelation
extends
ActiveQuery
implements
ActiveRelationInterface
class
ActiveRelation
extends
ActiveQuery
implements
ActiveRelationInterface
{
{
use
ActiveRelationTrait
;
use
ActiveRelationTrait
;
/**
* @inheritdoc
*/
protected
function
buildCursor
(
$db
=
null
)
{
if
(
$this
->
primaryModel
!==
null
)
{
// lazy loading
if
(
$this
->
via
instanceof
self
)
{
// via pivot collection
$viaModels
=
$this
->
via
->
findPivotRows
([
$this
->
primaryModel
]);
$this
->
filterByModels
(
$viaModels
);
}
elseif
(
is_array
(
$this
->
via
))
{
// via relation
/** @var ActiveRelation $viaQuery */
list
(
$viaName
,
$viaQuery
)
=
$this
->
via
;
if
(
$viaQuery
->
multiple
)
{
$viaModels
=
$viaQuery
->
all
();
$this
->
primaryModel
->
populateRelation
(
$viaName
,
$viaModels
);
}
else
{
$model
=
$viaQuery
->
one
();
$this
->
primaryModel
->
populateRelation
(
$viaName
,
$model
);
$viaModels
=
$model
===
null
?
[]
:
[
$model
];
}
$this
->
filterByModels
(
$viaModels
);
}
else
{
$this
->
filterByModels
([
$this
->
primaryModel
]);
}
}
return
parent
::
buildCursor
(
$db
);
}
}
}
\ No newline at end of file
tests/unit/extensions/mongodb/ActiveRelationTest.php
View file @
9ea4ef13
...
@@ -52,7 +52,7 @@ class ActiveRelationTest extends MongoDbTestCase
...
@@ -52,7 +52,7 @@ class ActiveRelationTest extends MongoDbTestCase
];
];
$customerOrders
[]
=
[
$customerOrders
[]
=
[
'customer_id'
=>
$customer
[
'_id'
],
'customer_id'
=>
$customer
[
'_id'
],
'number'
=>
$customer
[
'status'
]
+
1
,
'number'
=>
$customer
[
'status'
]
+
1
00
,
];
];
}
}
$customerOrderCollection
->
batchInsert
(
$customerOrders
);
$customerOrderCollection
->
batchInsert
(
$customerOrders
);
...
@@ -65,9 +65,10 @@ class ActiveRelationTest extends MongoDbTestCase
...
@@ -65,9 +65,10 @@ class ActiveRelationTest extends MongoDbTestCase
/** @var CustomerOrder $order */
/** @var CustomerOrder $order */
$order
=
CustomerOrder
::
find
([
'number'
=>
2
]);
$order
=
CustomerOrder
::
find
([
'number'
=>
2
]);
$this
->
assertFalse
(
$order
->
isRelationPopulated
(
'customer'
));
$this
->
assertFalse
(
$order
->
isRelationPopulated
(
'customer'
));
$
index
=
$order
->
customer
;
$
customer
=
$order
->
customer
;
$this
->
assertTrue
(
$order
->
isRelationPopulated
(
'customer'
));
$this
->
assertTrue
(
$order
->
isRelationPopulated
(
'customer'
));
$this
->
assertTrue
(
$index
instanceof
Customer
);
$this
->
assertTrue
(
$customer
instanceof
Customer
);
$this
->
assertEquals
((
string
)
$customer
->
_id
,
(
string
)
$order
->
customer_id
);
$this
->
assertEquals
(
1
,
count
(
$order
->
populatedRelations
));
$this
->
assertEquals
(
1
,
count
(
$order
->
populatedRelations
));
}
}
...
@@ -78,6 +79,8 @@ class ActiveRelationTest extends MongoDbTestCase
...
@@ -78,6 +79,8 @@ class ActiveRelationTest extends MongoDbTestCase
$this
->
assertTrue
(
$orders
[
0
]
->
isRelationPopulated
(
'customer'
));
$this
->
assertTrue
(
$orders
[
0
]
->
isRelationPopulated
(
'customer'
));
$this
->
assertTrue
(
$orders
[
1
]
->
isRelationPopulated
(
'customer'
));
$this
->
assertTrue
(
$orders
[
1
]
->
isRelationPopulated
(
'customer'
));
$this
->
assertTrue
(
$orders
[
0
]
->
customer
instanceof
Customer
);
$this
->
assertTrue
(
$orders
[
0
]
->
customer
instanceof
Customer
);
$this
->
assertEquals
((
string
)
$orders
[
0
]
->
customer
->
_id
,
(
string
)
$orders
[
0
]
->
customer_id
);
$this
->
assertTrue
(
$orders
[
1
]
->
customer
instanceof
Customer
);
$this
->
assertTrue
(
$orders
[
1
]
->
customer
instanceof
Customer
);
$this
->
assertEquals
((
string
)
$orders
[
1
]
->
customer
->
_id
,
(
string
)
$orders
[
1
]
->
customer_id
);
}
}
}
}
\ No newline at end of file
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