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
0adcd06c
Commit
0adcd06c
authored
Dec 23, 2014
by
Paul Klimov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixed lazy load of relations to `yii\mongodb\file\ActiveRecord`
parent
cb7131ed
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
127 additions
and
1 deletion
+127
-1
CHANGELOG.md
extensions/mongodb/CHANGELOG.md
+1
-1
ActiveQuery.php
extensions/mongodb/file/ActiveQuery.php
+32
-0
Customer.php
tests/unit/data/ar/mongodb/Customer.php
+8
-0
ActiveRelationTest.php
tests/unit/extensions/mongodb/file/ActiveRelationTest.php
+86
-0
No files found.
extensions/mongodb/CHANGELOG.md
View file @
0adcd06c
...
...
@@ -4,7 +4,7 @@ Yii Framework 2 mongodb extension Change Log
2.
0.2 under development
-----------------------
-
no changes in this release.
-
Bug #6376: Fixed lazy load of relations to
`yii\mongodb\file\ActiveRecord`
(klimov-paul)
2.0.1 December 07, 2014
...
...
extensions/mongodb/file/ActiveQuery.php
View file @
0adcd06c
...
...
@@ -70,6 +70,38 @@ class ActiveQuery extends Query implements ActiveQueryInterface
}
/**
* @inheritdoc
*/
protected
function
buildCursor
(
$db
=
null
)
{
if
(
$this
->
primaryModel
!==
null
)
{
// lazy loading
if
(
$this
->
via
instanceof
self
)
{
// via pivot collection
$viaModels
=
$this
->
via
->
findJunctionRows
([
$this
->
primaryModel
]);
$this
->
filterByModels
(
$viaModels
);
}
elseif
(
is_array
(
$this
->
via
))
{
// via relation
/* @var $viaQuery ActiveQuery */
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
);
}
/**
* Executes query and returns all results as an array.
* @param \yii\mongodb\Connection $db the Mongo connection used to execute the query.
* If null, the Mongo connection returned by [[modelClass]] will be used.
...
...
tests/unit/data/ar/mongodb/Customer.php
View file @
0adcd06c
...
...
@@ -2,6 +2,8 @@
namespace
yiiunit\data\ar\mongodb
;
use
yiiunit\data\ar\mongodb\file\CustomerFile
;
class
Customer
extends
ActiveRecord
{
public
static
function
collectionName
()
...
...
@@ -17,6 +19,7 @@ class Customer extends ActiveRecord
'email'
,
'address'
,
'status'
,
'file_id'
,
];
}
...
...
@@ -25,6 +28,11 @@ class Customer extends ActiveRecord
return
$this
->
hasMany
(
CustomerOrder
::
className
(),
[
'customer_id'
=>
'_id'
]);
}
public
function
getFile
()
{
return
$this
->
hasOne
(
CustomerFile
::
className
(),
[
'_id'
=>
'file_id'
]);
}
/**
* @inheritdoc
* @return CustomerQuery
...
...
tests/unit/extensions/mongodb/file/ActiveRelationTest.php
0 → 100644
View file @
0adcd06c
<?php
namespace
yiiunit\extensions\mongodb\file
;
use
yiiunit\data\ar\mongodb\Customer
;
use
yiiunit\data\ar\mongodb\file\CustomerFile
;
use
yiiunit\extensions\mongodb\MongoDbTestCase
;
/**
* @group mongodb
*/
class
ActiveRelationTest
extends
MongoDbTestCase
{
protected
function
setUp
()
{
parent
::
setUp
();
\yiiunit\data\ar\mongodb\ActiveRecord
::
$db
=
$this
->
getConnection
();
\yiiunit\data\ar\mongodb\file\ActiveRecord
::
$db
=
$this
->
getConnection
();
$this
->
setUpTestRows
();
}
protected
function
tearDown
()
{
$this
->
dropCollection
(
Customer
::
collectionName
());
$this
->
dropCollection
(
CustomerFile
::
collectionName
());
parent
::
tearDown
();
}
/**
* Sets up test rows.
*/
protected
function
setUpTestRows
()
{
$fileCollection
=
$this
->
getConnection
()
->
getFileCollection
(
CustomerFile
::
collectionName
());
$customers
=
[];
$files
=
[];
for
(
$i
=
1
;
$i
<=
5
;
$i
++
)
{
$file
=
[
'tag'
=>
'tag'
.
$i
,
'status'
=>
$i
,
];
$content
=
'content'
.
$i
;
$file
[
'_id'
]
=
$fileCollection
->
insertFileContent
(
$content
,
$file
);
$file
[
'content'
]
=
$content
;
$files
[]
=
$file
;
$customers
[]
=
[
'name'
=>
'name'
.
$i
,
'email'
=>
'email'
.
$i
,
'address'
=>
'address'
.
$i
,
'status'
=>
$i
,
'file_id'
=>
$file
[
'_id'
],
];
}
$customerCollection
=
$this
->
getConnection
()
->
getCollection
(
Customer
::
collectionName
());
$customers
=
$customerCollection
->
batchInsert
(
$customers
);
}
// Tests :
public
function
testFindLazy
()
{
/* @var $customer Customer */
$customer
=
Customer
::
findOne
([
'status'
=>
2
]);
$this
->
assertFalse
(
$customer
->
isRelationPopulated
(
'file'
));
$file
=
$customer
->
file
;
$this
->
assertTrue
(
$customer
->
isRelationPopulated
(
'file'
));
$this
->
assertTrue
(
$file
instanceof
CustomerFile
);
$this
->
assertEquals
((
string
)
$file
->
_id
,
(
string
)
$customer
->
file_id
);
$this
->
assertEquals
(
1
,
count
(
$customer
->
relatedRecords
));
}
public
function
testFindEager
()
{
/* @var $customers Customer[] */
$customers
=
Customer
::
find
()
->
with
(
'file'
)
->
all
();
$this
->
assertEquals
(
5
,
count
(
$customers
));
$this
->
assertTrue
(
$customers
[
0
]
->
isRelationPopulated
(
'file'
));
$this
->
assertTrue
(
$customers
[
1
]
->
isRelationPopulated
(
'file'
));
$this
->
assertTrue
(
$customers
[
0
]
->
file
instanceof
CustomerFile
);
$this
->
assertEquals
((
string
)
$customers
[
0
]
->
file
->
_id
,
(
string
)
$customers
[
0
]
->
file_id
);
$this
->
assertTrue
(
$customers
[
1
]
->
file
instanceof
CustomerFile
);
$this
->
assertEquals
((
string
)
$customers
[
1
]
->
file
->
_id
,
(
string
)
$customers
[
1
]
->
file_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