1717
1818trait RelationTrait
1919{
20-
2120 public function loadAll ($ POST , $ skippedRelations = [])
2221 {
2322 if ($ this ->load ($ POST )) {
@@ -27,7 +26,7 @@ public function loadAll($POST, $skippedRelations = [])
2726 /* @var $rel ActiveQuery */
2827 /* @var $this ActiveRecord */
2928 /* @var $relObj ActiveRecord */
30- $ isHasMany = is_array ($ value );
29+ $ isHasMany = is_array ($ value ) && is_array ( current ( $ value )) ;
3130 $ relName = ($ isHasMany ) ? lcfirst (Inflector::pluralize ($ key )) : lcfirst ($ key );
3231 $ rel = $ this ->getRelation ($ relName );
3332 $ relModelClass = $ rel ->modelClass ;
@@ -68,8 +67,8 @@ public function loadAll($POST, $skippedRelations = [])
6867 }
6968 $ this ->populateRelation ($ relName , $ container );
7069 } else {
71- $ relObj = (empty ($ relPost [$ relPKAttr [0 ]])) ? new $ relModelClass : $ relModelClass ::findOne ($ relPost [$ relPKAttr [0 ]]);
72- $ relObj ->load ($ value );
70+ $ relObj = (empty ($ value [$ relPKAttr [0 ]])) ? new $ relModelClass : $ relModelClass ::findOne ($ value [$ relPKAttr [0 ]]);
71+ $ relObj ->load ($ value, '' );
7372 $ this ->populateRelation ($ relName , $ relObj );
7473 }
7574 }
@@ -92,26 +91,24 @@ public function saveAll($skippedRelations = [])
9291 if (!empty ($ this ->relatedRecords )) {
9392 foreach ($ this ->relatedRecords as $ name => $ records ) {
9493
95- if (in_array ($ name ,$ skippedRelations ))
94+ if (in_array ($ name , $ skippedRelations ))
9695 continue ;
9796
9897 if (!empty ($ records )) {
99- $ isHasMany = is_array ($ records );
10098 $ AQ = $ this ->getRelation ($ name );
10199 $ link = $ AQ ->link ;
102100 $ notDeletedPK = [];
103- $ relPKAttr = $ records [0 ]->primaryKey ();
101+ $ relPKAttr = ( $ AQ -> multiple ) ? $ records [0 ]-> primaryKey () : $ records ->primaryKey ();
104102 $ isManyMany = (count ($ relPKAttr ) > 1 );
105-
106- if ($ isHasMany ) {
103+ if ($ AQ ->multiple ) {
107104 /* @var $relModel ActiveRecord */
108105 $ i = 0 ;
109106 foreach ($ records as $ index => $ relModel ) {
110107 $ notDeletedFK = [];
111108 foreach ($ link as $ key => $ value ) {
112109 $ relModel ->$ key = $ this ->$ value ;
113110 if ($ isManyMany ) $ notDeletedFK [$ key ] = $ this ->$ value ;
114- elseif ($ isHasMany ) $ notDeletedFK [$ key ] = "$ key = ' {$ this ->$ value }' " ;
111+ elseif ($ AQ -> multiple ) $ notDeletedFK [$ key ] = "$ key = ' {$ this ->$ value }' " ;
115112 }
116113 $ relSave = $ relModel ->save ();
117114
@@ -157,7 +154,7 @@ public function saveAll($skippedRelations = [])
157154 try {
158155 $ relModel ->deleteAll ($ compiledNotDeletedPK );
159156 } catch (\yii \db \IntegrityException $ exc ) {
160- $ this ->addError ($ name , "Data can't be deleted because it's still used by another data. " );
157+ $ this ->addError ($ name , \Yii:: t ( ' app ' , "Data can't be deleted because it's still used by another data. " ) );
161158 $ error = true ;
162159 }
163160 } else {
@@ -166,18 +163,29 @@ public function saveAll($skippedRelations = [])
166163 if (!empty ($ compiledNotDeletedPK )) {
167164 try {
168165 $ relModel ->deleteAll ($ notDeletedFK . ' AND ' . $ relPKAttr [0 ] . " NOT IN ( $ compiledNotDeletedPK) " );
169-
170166 } catch (\yii \db \IntegrityException $ exc ) {
171- $ this ->addError ($ name , "Data can't be deleted because it's still used by another data. " );
167+ $ this ->addError ($ name , \Yii:: t ( ' app ' , "Data can't be deleted because it's still used by another data. " ) );
172168 $ error = true ;
173169 }
174170 }
175171 }
176172 }
177173 } else {
178174 //Has One
175+ foreach ($ link as $ key => $ value ) {
176+ $ records ->$ key = $ this ->$ value ;
177+ }
178+ $ relSave = $ records ->save ();
179+ if (!$ relSave || !empty ($ records ->errors )) {
180+ $ recordsWords = Inflector::camel2words (StringHelper::basename ($ AQ ->modelClass ));
181+ foreach ($ records ->errors as $ validation ) {
182+ foreach ($ validation as $ errorMsg ) {
183+ $ this ->addError ($ name , "$ recordsWords : $ errorMsg " );
184+ }
185+ }
186+ $ error = true ;
187+ }
179188 }
180-
181189 }
182190 }
183191 } else {
@@ -187,7 +195,6 @@ public function saveAll($skippedRelations = [])
187195 foreach ($ relData as $ rel ) {
188196 if (in_array ($ rel ['name ' ], $ skippedRelations ))
189197 continue ;
190-
191198 /* @var $relModel ActiveRecord */
192199 if (empty ($ rel ['via ' ])){
193200 $ relModel = new $ rel ['modelClass ' ];
@@ -200,7 +207,7 @@ public function saveAll($skippedRelations = [])
200207 try {
201208 $ relModel ->deleteAll (implode (" AND " , $ condition ));
202209 } catch (\yii \db \IntegrityException $ exc ) {
203- $ this ->addError ($ rel ['name ' ], "Data can't be deleted because it's still used by another data. " );
210+ $ this ->addError ($ rel ['name ' ], \Yii:: t ( ' app ' , "Data can't be deleted because it's still used by another data. " ) );
204211 $ error = true ;
205212 }
206213 }else {
@@ -211,7 +218,7 @@ public function saveAll($skippedRelations = [])
211218 try {
212219 $ relModel ->deleteAll (implode (" AND " , $ condition ));
213220 } catch (\yii \db \IntegrityException $ exc ) {
214- $ this ->addError ($ rel ['name ' ], "Data can't be deleted because it's still used by another data. " );
221+ $ this ->addError ($ rel ['name ' ], \Yii:: t ( ' app ' , "Data can't be deleted because it's still used by another data. " ) );
215222 $ error = true ;
216223 }
217224 }
@@ -313,6 +320,9 @@ public function getRelationData()
313320 if ($ method ->name === 'deleteWithRelated ' ) {
314321 continue ;
315322 }
323+ if (strpos ($ method ->name , 'get ' ) === false ) {
324+ continue ;
325+ }
316326 try {
317327 $ rel = call_user_func (array ($ this , $ method ->name ));
318328 if ($ rel instanceof \yii \db \ActiveQuery) {
@@ -338,11 +348,15 @@ public function getAttributesWithRelatedAsPost()
338348 $ return = [];
339349 $ shortName = StringHelper::basename (get_class ($ this ));
340350 $ return [$ shortName ] = $ this ->attributes ;
341- foreach ($ this ->relatedRecords as $ records ) {
342- foreach ($ records as $ index => $ record ) {
343- $ shortNameRel = StringHelper::basename (get_class ($ record ));
344- $ return [$ shortNameRel ][$ index ] = $ record ->attributes ;
351+ foreach ($ this ->relatedRecords as $ name => $ records ) {
352+ if (is_array ($ records ) && is_array (current ($ records ))){
353+ foreach ($ records as $ index => $ record ) {
354+ $ return [$ name ][$ index ] = $ record ->attributes ;
355+ }
356+ }else {
357+ $ return [$ name ] = $ records ->attributes ;
345358 }
359+
346360 }
347361 return $ return ;
348362 }
@@ -351,11 +365,14 @@ public function getAttributesWithRelated()
351365 {
352366 $ return = $ this ->attributes ;
353367 foreach ($ this ->relatedRecords as $ name => $ records ) {
354- foreach ($ records as $ index => $ record ) {
355- $ return [$ name ][$ index ] = $ record ->attributes ;
368+ if (is_array ($ records ) && is_array (current ($ records ))){
369+ foreach ($ records as $ index => $ record ) {
370+ $ return [$ name ][$ index ] = $ record ->attributes ;
371+ }
372+ }else {
373+ $ return [$ name ] = $ records ->attributes ;
356374 }
357375 }
358376 return $ return ;
359377 }
360-
361378}
0 commit comments