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
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Rotua Panjaitan
yii2
Commits
9b8ffdfa
Commit
9b8ffdfa
authored
Mar 06, 2014
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes #2653: Fixed the bug that unsetting an unpopulated AR relation would…
Fixes #2653: Fixed the bug that unsetting an unpopulated AR relation would trigger exception (qiangxue)
parent
11baf619
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
44 additions
and
9 deletions
+44
-9
CHANGELOG.md
framework/CHANGELOG.md
+1
-0
ActiveRecordInterface.php
framework/db/ActiveRecordInterface.php
+2
-1
BaseActiveRecord.php
framework/db/BaseActiveRecord.php
+32
-6
ActiveRecordTestTrait.php
tests/unit/framework/ar/ActiveRecordTestTrait.php
+9
-2
No files found.
framework/CHANGELOG.md
View file @
9b8ffdfa
...
...
@@ -52,6 +52,7 @@ Yii Framework 2 Change Log
-
Bug #2559: Going back on browser history breaks GridView filtering with
`Pjax`
(tonydspaniard)
-
Bug #2607:
`yii message`
tool wasn't updating
`message`
table (mitalcoi)
-
Bug #2624: Html::textArea() should respect "name" option. (qiangxue)
-
Bug #2653: Fixed the bug that unsetting an unpopulated AR relation would trigger exception (qiangxue)
-
Bug: Fixed
`Call to a member function registerAssetFiles() on a non-object`
in case of wrong
`sourcePath`
for an asset bundle (samdark)
-
Bug: Fixed incorrect event name for
`yii\jui\Spinner`
(samdark)
-
Bug: Json::encode() did not handle objects that implement JsonSerializable interface correctly (cebe)
...
...
framework/db/ActiveRecordInterface.php
View file @
9b8ffdfa
...
...
@@ -272,9 +272,10 @@ interface ActiveRecordInterface
* (normally this would be a relational [[ActiveQuery]] object).
* It can be declared in either the ActiveRecord class itself or one of its behaviors.
* @param string $name the relation name
* @param boolean $throwException whether to throw exception if the relation does not exist.
* @return ActiveQueryInterface the relational query object
*/
public
function
getRelation
(
$name
);
public
function
getRelation
(
$name
,
$throwException
=
true
);
/**
* Establishes the relationship between two records.
...
...
framework/db/BaseActiveRecord.php
View file @
9b8ffdfa
...
...
@@ -305,7 +305,7 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
unset
(
$this
->
_attributes
[
$name
]);
}
elseif
(
array_key_exists
(
$name
,
$this
->
_related
))
{
unset
(
$this
->
_related
[
$name
]);
}
else
{
}
else
if
(
$this
->
getRelation
(
$name
,
false
)
===
null
)
{
parent
::
__unset
(
$name
);
}
}
...
...
@@ -1063,20 +1063,30 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
* A relation is defined by a getter method which returns an [[ActiveQueryInterface]] object.
* It can be declared in either the Active Record class itself or one of its behaviors.
* @param string $name the relation name
* @return ActiveQueryInterface|ActiveQuery the relational query object
* @param boolean $throwException whether to throw exception if the relation does not exist.
* @return ActiveQueryInterface|ActiveQuery the relational query object. If the relation does not exist
* and `$throwException` is false, null will be returned.
* @throws InvalidParamException if the named relation does not exist.
*/
public
function
getRelation
(
$name
)
public
function
getRelation
(
$name
,
$throwException
=
true
)
{
$getter
=
'get'
.
$name
;
try
{
// the relation could be defined in a behavior
$relation
=
$this
->
$getter
();
}
catch
(
UnknownMethodException
$e
)
{
throw
new
InvalidParamException
(
get_class
(
$this
)
.
' has no relation named "'
.
$name
.
'".'
,
0
,
$e
);
if
(
$throwException
)
{
throw
new
InvalidParamException
(
get_class
(
$this
)
.
' has no relation named "'
.
$name
.
'".'
,
0
,
$e
);
}
else
{
return
null
;
}
}
if
(
!
$relation
instanceof
ActiveQueryInterface
)
{
throw
new
InvalidParamException
(
get_class
(
$this
)
.
' has no relation named "'
.
$name
.
'".'
);
if
(
$throwException
)
{
throw
new
InvalidParamException
(
get_class
(
$this
)
.
' has no relation named "'
.
$name
.
'".'
);
}
else
{
return
null
;
}
}
if
(
method_exists
(
$this
,
$getter
))
{
...
...
@@ -1084,7 +1094,11 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
$method
=
new
\ReflectionMethod
(
$this
,
$getter
);
$realName
=
lcfirst
(
substr
(
$method
->
getName
(),
3
));
if
(
$realName
!==
$name
)
{
throw
new
InvalidParamException
(
'Relation names are case sensitive. '
.
get_class
(
$this
)
.
" has a relation named
\"
$realName
\"
instead of
\"
$name
\"
."
);
if
(
$throwException
)
{
throw
new
InvalidParamException
(
'Relation names are case sensitive. '
.
get_class
(
$this
)
.
" has a relation named
\"
$realName
\"
instead of
\"
$name
\"
."
);
}
else
{
return
null
;
}
}
}
...
...
@@ -1367,4 +1381,16 @@ abstract class BaseActiveRecord extends Model implements ActiveRecordInterface
$fields
=
array_keys
(
$this
->
getRelatedRecords
());
return
array_combine
(
$fields
,
$fields
);
}
/**
* Sets the element value at the specified offset to null.
* This method is required by the SPL interface `ArrayAccess`.
* It is implicitly called when you use something like `unset($model[$offset])`.
* @param mixed $offset the offset to unset element
*/
public
function
offsetUnset
(
$offset
)
{
// use unset to trigger __unset()
unset
(
$this
->
$offset
);
}
}
tests/unit/framework/ar/ActiveRecordTestTrait.php
View file @
9b8ffdfa
...
...
@@ -184,7 +184,7 @@ trait ActiveRecordTestTrait
$this
->
assertEquals
([
'user3'
,
'user2'
,
'user1'
],
$this
->
callCustomerFind
()
->
orderBy
([
'name'
=>
SORT_DESC
])
->
column
(
'name'
));
}
public
function
test
f
indIndexBy
()
public
function
test
F
indIndexBy
()
{
$customerClass
=
$this
->
getCustomerClass
();
/** @var TestCase|ActiveRecordTestTrait $this */
...
...
@@ -205,7 +205,7 @@ trait ActiveRecordTestTrait
$this
->
assertTrue
(
$customers
[
'3-user3'
]
instanceof
$customerClass
);
}
public
function
test
f
indIndexByAsArray
()
public
function
test
F
indIndexByAsArray
()
{
/** @var TestCase|ActiveRecordTestTrait $this */
// indexBy + asArray
...
...
@@ -399,6 +399,10 @@ trait ActiveRecordTestTrait
$this
->
assertEquals
(
2
,
count
(
$orders
));
$this
->
assertEquals
(
1
,
count
(
$customer
->
relatedRecords
));
// unset
unset
(
$customer
[
'orders'
]);
$this
->
assertFalse
(
$customer
->
isRelationPopulated
(
'orders'
));
/** @var Customer $customer */
$customer
=
$this
->
callCustomerFind
(
2
);
$this
->
assertFalse
(
$customer
->
isRelationPopulated
(
'orders'
));
...
...
@@ -422,6 +426,9 @@ trait ActiveRecordTestTrait
$this
->
assertEquals
(
1
,
count
(
$customers
[
1
]
->
orders
));
$this
->
assertEquals
(
2
,
count
(
$customers
[
2
]
->
orders
));
$this
->
assertEquals
(
0
,
count
(
$customers
[
3
]
->
orders
));
// unset
unset
(
$customers
[
1
]
->
orders
);
$this
->
assertFalse
(
$customers
[
1
]
->
isRelationPopulated
(
'orders'
));
$customer
=
$this
->
callCustomerFind
()
->
where
([
'id'
=>
1
])
->
with
(
'orders'
)
->
one
();
$this
->
assertTrue
(
$customer
->
isRelationPopulated
(
'orders'
));
...
...
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