File tree Expand file tree Collapse file tree 2 files changed +24
-13
lines changed Expand file tree Collapse file tree 2 files changed +24
-13
lines changed Original file line number Diff line number Diff line change 44
55use Yii ;
66use yii \base \Behavior ;
7+ use yii \base \InvalidArgumentException ;
78use yii \base \InvalidConfigException ;
89use yii \base \UnknownPropertyException ;
910use yii \db \ActiveRecord ;
@@ -123,7 +124,7 @@ public function afterValidate()
123124 }
124125
125126 $ errors = [];
126- $ models = isset ($ this ->_inserteds [ $ name ]) ? $ this -> _inserteds [ $ name] : [] ;
127+ $ models = ArrayHelper:: getValue ($ this ->_inserteds , $ name, []) ;
127128
128129 foreach ($ models as $ model ) {
129130 if (!$ model ->validate ()) {
@@ -149,6 +150,10 @@ public function __set($name, $value)
149150 try {
150151 parent ::__set ($ name , $ value );
151152 } catch (UnknownPropertyException $ exception ) {
153+ if (!is_array ($ value )) {
154+ throw new InvalidArgumentException ("The ' $ name' property must be an array " );
155+ }
156+
152157 $ definition = $ this ->findDefinition ($ name );
153158 if ($ definition !== null ) {
154159 $ this ->prepareRelation ($ definition , [$ name => $ value ]);
Original file line number Diff line number Diff line change 44
55use solutosoft \linkmany \LinkManyBehavior ;
66use solutosoft \linkmany \tests \models \Post ;
7- use solutosoft \linkmany \tests \models \Tag ;
87
9- //use solutosoft\linkmany\tests\models\PostLanguage;
10- //use solutosoft\linkmany\tests\models\Tag;
118
129class LinkManyBehaviorTest extends TestCase
1310{
14- public function testInitException ()
11+ public function testInitSuccess ()
1512 {
16- $ this ->setExpectedException ('\yii\base\InvalidConfigException ' );
17-
1813 new LinkManyBehavior ([
1914 'relations ' => [
20- ['invalid ' => true ]
15+ 'relation_1 ' ,
16+ 'relation_2 ' => [
17+ 'formName ' => 'test '
18+ ]
2119 ]
2220 ]);
2321 }
2422
25- public function testInitSuccess ()
23+ public function testInitException ()
2624 {
25+ $ this ->setExpectedException ('\yii\base\InvalidConfigException ' );
26+
2727 new LinkManyBehavior ([
2828 'relations ' => [
29- 'relation_1 ' ,
30- 'relation_2 ' => [
31- 'formName ' => 'test '
32- ]
29+ ['invalid ' => true ]
3330 ]
3431 ]);
3532 }
3633
34+ public function testInvalidRelationValue ()
35+ {
36+ $ this ->setExpectedException ('\yii\base\InvalidArgumentException ' );
37+
38+ $ post = new Post ();
39+ $ post ->comments = null ;
40+ }
41+
3742 public function testFillNewRecord ()
3843 {
3944 $ post = new Post ();
@@ -234,6 +239,7 @@ public function testSetRelation()
234239 $ post ->tags = [1 ];
235240
236241 $ post ->save ();
242+ $ post ->refresh ();
237243 $ this ->assertCount (1 , $ post ->tags );
238244 }
239245}
You can’t perform that action at this time.
0 commit comments