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
cc38e9c7
Commit
cc38e9c7
authored
Jul 21, 2014
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fixed issue with indexBy and array valued relations
parent
d5375e8c
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
52 additions
and
3 deletions
+52
-3
ActiveRelationTrait.php
framework/db/ActiveRelationTrait.php
+9
-3
Order.php
tests/unit/data/ar/Order.php
+6
-0
Order.php
tests/unit/data/ar/elasticsearch/Order.php
+6
-0
Order.php
tests/unit/data/ar/redis/Order.php
+6
-0
ActiveRecordTestTrait.php
tests/unit/framework/ar/ActiveRecordTestTrait.php
+25
-0
No files found.
framework/db/ActiveRelationTrait.php
View file @
cc38e9c7
...
@@ -62,6 +62,7 @@ trait ActiveRelationTrait
...
@@ -62,6 +62,7 @@ trait ActiveRelationTrait
*/
*/
public
$inverseOf
;
public
$inverseOf
;
/**
/**
* Clones internal objects.
* Clones internal objects.
*/
*/
...
@@ -106,7 +107,6 @@ trait ActiveRelationTrait
...
@@ -106,7 +107,6 @@ trait ActiveRelationTrait
if
(
$callable
!==
null
)
{
if
(
$callable
!==
null
)
{
call_user_func
(
$callable
,
$relation
);
call_user_func
(
$callable
,
$relation
);
}
}
return
$this
;
return
$this
;
}
}
...
@@ -133,7 +133,6 @@ trait ActiveRelationTrait
...
@@ -133,7 +133,6 @@ trait ActiveRelationTrait
public
function
inverseOf
(
$relationName
)
public
function
inverseOf
(
$relationName
)
{
{
$this
->
inverseOf
=
$relationName
;
$this
->
inverseOf
=
$relationName
;
return
$this
;
return
$this
;
}
}
...
@@ -244,7 +243,14 @@ trait ActiveRelationTrait
...
@@ -244,7 +243,14 @@ trait ActiveRelationTrait
$value
=
[];
$value
=
[];
foreach
(
$keys
as
$key
)
{
foreach
(
$keys
as
$key
)
{
if
(
isset
(
$buckets
[
$key
]))
{
if
(
isset
(
$buckets
[
$key
]))
{
$value
=
array_merge
(
$value
,
$buckets
[
$key
]);
if
(
$this
->
indexBy
!==
null
)
{
// if indexBy is set, array_merge will cause renumbering of numeric array
foreach
(
$buckets
[
$key
]
as
$bucketKey
=>
$bucketValue
)
{
$value
[
$bucketKey
]
=
$bucketValue
;
}
}
else
{
$value
=
array_merge
(
$value
,
$buckets
[
$key
]);
}
}
}
}
}
}
else
{
}
else
{
...
...
tests/unit/data/ar/Order.php
View file @
cc38e9c7
...
@@ -45,6 +45,12 @@ class Order extends ActiveRecord
...
@@ -45,6 +45,12 @@ class Order extends ActiveRecord
})
->
orderBy
(
'item.id'
);
})
->
orderBy
(
'item.id'
);
}
}
public
function
getItemsIndexed
()
{
return
$this
->
hasMany
(
Item
::
className
(),
[
'id'
=>
'item_id'
])
->
via
(
'orderItems'
)
->
indexBy
(
'id'
);
}
public
function
getItemsWithNullFK
()
public
function
getItemsWithNullFK
()
{
{
return
$this
->
hasMany
(
Item
::
className
(),
[
'id'
=>
'item_id'
])
return
$this
->
hasMany
(
Item
::
className
(),
[
'id'
=>
'item_id'
])
...
...
tests/unit/data/ar/elasticsearch/Order.php
View file @
cc38e9c7
...
@@ -48,6 +48,12 @@ class Order extends ActiveRecord
...
@@ -48,6 +48,12 @@ class Order extends ActiveRecord
->
via
(
'orderItems'
)
->
orderBy
(
'id'
);
->
via
(
'orderItems'
)
->
orderBy
(
'id'
);
}
}
public
function
getItemsIndexed
()
{
return
$this
->
hasMany
(
Item
::
className
(),
[
'id'
=>
'item_id'
])
->
via
(
'orderItems'
)
->
indexBy
(
'id'
);
}
public
function
getItemsWithNullFK
()
public
function
getItemsWithNullFK
()
{
{
return
$this
->
hasMany
(
Item
::
className
(),
[
'id'
=>
'item_id'
])
return
$this
->
hasMany
(
Item
::
className
(),
[
'id'
=>
'item_id'
])
...
...
tests/unit/data/ar/redis/Order.php
View file @
cc38e9c7
...
@@ -27,6 +27,12 @@ class Order extends ActiveRecord
...
@@ -27,6 +27,12 @@ class Order extends ActiveRecord
});
});
}
}
public
function
getItemsIndexed
()
{
return
$this
->
hasMany
(
Item
::
className
(),
[
'id'
=>
'item_id'
])
->
via
(
'orderItems'
)
->
indexBy
(
'id'
);
}
public
function
getItemsInOrder1
()
public
function
getItemsInOrder1
()
{
{
return
$this
->
hasMany
(
Item
::
className
(),
[
'id'
=>
'item_id'
])
return
$this
->
hasMany
(
Item
::
className
(),
[
'id'
=>
'item_id'
])
...
...
tests/unit/framework/ar/ActiveRecordTestTrait.php
View file @
cc38e9c7
...
@@ -1064,4 +1064,29 @@ trait ActiveRecordTestTrait
...
@@ -1064,4 +1064,29 @@ trait ActiveRecordTestTrait
$customers
=
$customerClass
::
find
()
->
where
([
'IN'
,
'id'
,
[]])
->
all
();
$customers
=
$customerClass
::
find
()
->
where
([
'IN'
,
'id'
,
[]])
->
all
();
$this
->
assertEquals
(
0
,
count
(
$customers
));
$this
->
assertEquals
(
0
,
count
(
$customers
));
}
}
public
function
testFindEagerIndexBy
()
{
/* @var $this TestCase|ActiveRecordTestTrait */
/* @var $orderClass \yii\db\ActiveRecordInterface */
$orderClass
=
$this
->
getOrderClass
();
/* @var $order Order */
$order
=
$orderClass
::
find
()
->
with
(
'itemsIndexed'
)
->
where
([
'id'
=>
1
])
->
one
();
$this
->
assertTrue
(
$order
->
isRelationPopulated
(
'itemsIndexed'
));
$items
=
$order
->
itemsIndexed
;
$this
->
assertEquals
(
2
,
count
(
$items
));
$this
->
assertTrue
(
isset
(
$items
[
1
]));
$this
->
assertTrue
(
isset
(
$items
[
2
]));
/* @var $order Order */
$order
=
$orderClass
::
find
()
->
with
(
'itemsIndexed'
)
->
where
([
'id'
=>
2
])
->
one
();
$this
->
assertTrue
(
$order
->
isRelationPopulated
(
'itemsIndexed'
));
$items
=
$order
->
itemsIndexed
;
$this
->
assertEquals
(
3
,
count
(
$items
));
$this
->
assertTrue
(
isset
(
$items
[
3
]));
$this
->
assertTrue
(
isset
(
$items
[
4
]));
$this
->
assertTrue
(
isset
(
$items
[
5
]));
}
}
}
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