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
d036d2f5
Commit
d036d2f5
authored
Jul 23, 2014
by
Carsten Brandt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
add support for array valued attr relations to unlink and link
parent
cc38e9c7
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
97 additions
and
2 deletions
+97
-2
ActiveRelationTrait.php
framework/db/ActiveRelationTrait.php
+1
-1
BaseActiveRecord.php
framework/db/BaseActiveRecord.php
+19
-1
ActiveRecordTest.php
tests/unit/extensions/elasticsearch/ActiveRecordTest.php
+77
-0
No files found.
framework/db/ActiveRelationTrait.php
View file @
d036d2f5
...
...
@@ -239,7 +239,7 @@ trait ActiveRelationTrait
$link
=
array_values
(
isset
(
$viaQuery
)
?
$viaQuery
->
link
:
$this
->
link
);
foreach
(
$primaryModels
as
$i
=>
$primaryModel
)
{
if
(
$this
->
multiple
&&
count
(
$link
)
<
2
&&
is_array
(
$keys
=
$primaryModel
->
{
reset
(
$link
)}))
{
if
(
$this
->
multiple
&&
count
(
$link
)
==
1
&&
is_array
(
$keys
=
$primaryModel
->
{
reset
(
$link
)}))
{
$value
=
[];
foreach
(
$keys
as
$key
)
{
if
(
isset
(
$buckets
[
$key
]))
{
...
...
framework/db/BaseActiveRecord.php
View file @
d036d2f5
...
...
@@ -1282,9 +1282,17 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
}
$delete
?
$model
->
delete
()
:
$model
->
save
(
false
);
}
elseif
(
$p1
)
{
foreach
(
$relation
->
link
as
$b
)
{
foreach
(
$relation
->
link
as
$a
=>
$b
)
{
if
(
is_array
(
$this
->
$b
))
{
// relation via array valued attribute
if
((
$key
=
array_search
(
$model
->
$a
,
$this
->
$b
,
false
))
!==
false
)
{
$values
=
$this
->
$b
;
unset
(
$values
[
$key
]);
$this
->
$b
=
$values
;
}
}
else
{
$this
->
$b
=
null
;
}
}
$delete
?
$this
->
delete
()
:
$this
->
save
(
false
);
}
else
{
throw
new
InvalidCallException
(
'Unable to unlink models: the link does not involve any primary key.'
);
...
...
@@ -1354,6 +1362,11 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
}
else
{
/* @var $relatedModel ActiveRecordInterface */
$relatedModel
=
$relation
->
modelClass
;
if
(
!
$delete
&&
count
(
$relation
->
link
)
==
1
&&
is_array
(
$this
->
{
$b
=
reset
(
$relation
->
link
)}))
{
// relation via array valued attribute
$this
->
$b
=
[];
$this
->
save
(
false
);
}
else
{
$nulls
=
[];
$condition
=
[];
foreach
(
$relation
->
link
as
$a
=>
$b
)
{
...
...
@@ -1366,6 +1379,7 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
$relatedModel
::
updateAll
(
$nulls
,
$condition
);
}
}
}
unset
(
$this
->
_related
[
$name
]);
}
...
...
@@ -1383,8 +1397,12 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
if
(
$value
===
null
)
{
throw
new
InvalidCallException
(
'Unable to link models: the primary key of '
.
get_class
(
$primaryModel
)
.
' is null.'
);
}
if
(
is_array
(
$foreignModel
->
$fk
))
{
// relation via array valued attribute
$foreignModel
->
$fk
=
array_merge
(
$foreignModel
->
$fk
,
[
$value
]);
}
else
{
$foreignModel
->
$fk
=
$value
;
}
}
$foreignModel
->
save
(
false
);
}
...
...
tests/unit/extensions/elasticsearch/ActiveRecordTest.php
View file @
d036d2f5
...
...
@@ -738,6 +738,7 @@ class ActiveRecordTest extends ElasticSearchTestCase
/* @var $order Order */
$order
=
Order
::
find
()
->
with
(
'itemsByArrayValue'
)
->
where
([
'id'
=>
2
])
->
one
();
$this
->
assertTrue
(
$order
->
isRelationPopulated
(
'itemsByArrayValue'
));
$items
=
$order
->
itemsByArrayValue
;
$this
->
assertEquals
(
3
,
count
(
$items
));
$this
->
assertTrue
(
isset
(
$items
[
3
]));
...
...
@@ -748,5 +749,81 @@ class ActiveRecordTest extends ElasticSearchTestCase
$this
->
assertTrue
(
$items
[
5
]
instanceof
Item
);
}
public
function
testArrayAttributeRelationLink
()
{
/* @var $order Order */
$order
=
Order
::
find
()
->
where
([
'id'
=>
1
])
->
one
();
$items
=
$order
->
itemsByArrayValue
;
$this
->
assertEquals
(
2
,
count
(
$items
));
$this
->
assertTrue
(
isset
(
$items
[
1
]));
$this
->
assertTrue
(
isset
(
$items
[
2
]));
$item
=
Item
::
get
(
5
);
$order
->
link
(
'itemsByArrayValue'
,
$item
);
$this
->
afterSave
();
$items
=
$order
->
itemsByArrayValue
;
$this
->
assertEquals
(
3
,
count
(
$items
));
$this
->
assertTrue
(
isset
(
$items
[
1
]));
$this
->
assertTrue
(
isset
(
$items
[
2
]));
$this
->
assertTrue
(
isset
(
$items
[
5
]));
// check also after refresh
$this
->
assertTrue
(
$order
->
refresh
());
$items
=
$order
->
itemsByArrayValue
;
$this
->
assertEquals
(
3
,
count
(
$items
));
$this
->
assertTrue
(
isset
(
$items
[
1
]));
$this
->
assertTrue
(
isset
(
$items
[
2
]));
$this
->
assertTrue
(
isset
(
$items
[
5
]));
}
public
function
testArrayAttributeRelationUnLink
()
{
/* @var $order Order */
$order
=
Order
::
find
()
->
where
([
'id'
=>
1
])
->
one
();
$items
=
$order
->
itemsByArrayValue
;
$this
->
assertEquals
(
2
,
count
(
$items
));
$this
->
assertTrue
(
isset
(
$items
[
1
]));
$this
->
assertTrue
(
isset
(
$items
[
2
]));
$item
=
Item
::
get
(
2
);
$order
->
unlink
(
'itemsByArrayValue'
,
$item
);
$this
->
afterSave
();
$items
=
$order
->
itemsByArrayValue
;
$this
->
assertEquals
(
1
,
count
(
$items
));
$this
->
assertTrue
(
isset
(
$items
[
1
]));
$this
->
assertFalse
(
isset
(
$items
[
2
]));
// check also after refresh
$this
->
assertTrue
(
$order
->
refresh
());
$items
=
$order
->
itemsByArrayValue
;
$this
->
assertEquals
(
1
,
count
(
$items
));
$this
->
assertTrue
(
isset
(
$items
[
1
]));
$this
->
assertFalse
(
isset
(
$items
[
2
]));
}
public
function
testArrayAttributeRelationUnLinkAll
()
{
/* @var $order Order */
$order
=
Order
::
find
()
->
where
([
'id'
=>
1
])
->
one
();
$items
=
$order
->
itemsByArrayValue
;
$this
->
assertEquals
(
2
,
count
(
$items
));
$this
->
assertTrue
(
isset
(
$items
[
1
]));
$this
->
assertTrue
(
isset
(
$items
[
2
]));
$order
->
unlinkAll
(
'itemsByArrayValue'
);
$this
->
afterSave
();
$items
=
$order
->
itemsByArrayValue
;
$this
->
assertEquals
(
0
,
count
(
$items
));
// check also after refresh
$this
->
assertTrue
(
$order
->
refresh
());
$items
=
$order
->
itemsByArrayValue
;
$this
->
assertEquals
(
0
,
count
(
$items
));
}
// TODO test AR with not mapped PK
}
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