1010namespace mootensai \relation ;
1111
1212use Yii ;
13+ use yii \base \ErrorException ;
1314use yii \db \ActiveQuery ;
1415use \yii \db \ActiveRecord ;
1516use \yii \db \Exception ;
17+ use yii \db \IntegrityException ;
1618use \yii \helpers \Inflector ;
1719use \yii \helpers \StringHelper ;
1820use yii \helpers \ArrayHelper ;
@@ -37,6 +39,7 @@ public function loadAll($POST, $skippedRelations = [])
3739 }
3840
3941 $ AQ = $ this ->getRelation ($ relName );
42+ /* @var $relModelClass ActiveRecord */
4043 $ relModelClass = $ AQ ->modelClass ;
4144 $ relPKAttr = $ relModelClass ::primaryKey ();
4245 $ isManyMany = count ($ relPKAttr ) > 1 ;
@@ -97,6 +100,7 @@ public function saveAll($skippedRelations = [])
97100 if ($ this ->save ()) {
98101 $ error = false ;
99102 if (!empty ($ this ->relatedRecords )) {
103+ /* @var $records ActiveRecord */
100104 foreach ($ this ->relatedRecords as $ name => $ records ) {
101105
102106 if (in_array ($ name , $ skippedRelations ))
@@ -106,20 +110,20 @@ public function saveAll($skippedRelations = [])
106110 $ AQ = $ this ->getRelation ($ name );
107111 $ link = $ AQ ->link ;
108112 $ notDeletedPK = [];
113+ $ notDeletedFK = [];
109114 $ relPKAttr = ($ AQ ->multiple ) ? $ records [0 ]->primaryKey () : $ records ->primaryKey ();
110115 $ isManyMany = (count ($ relPKAttr ) > 1 );
111116 if ($ AQ ->multiple ) {
112117 /* @var $relModel ActiveRecord */
113118 foreach ($ records as $ index => $ relModel ) {
114- $ notDeletedFK = [];
115119 foreach ($ link as $ key => $ value ) {
116120 $ relModel ->$ key = $ this ->$ value ;
117121 $ notDeletedFK [$ key ] = $ this ->$ value ;
118122 }
119123 $ relSave = $ relModel ->save ();
120124
121125 if (!$ relSave || !empty ($ relModel ->errors )) {
122- $ relModelWords = \ Yii::t ('app ' , Inflector::camel2words (StringHelper::basename ($ AQ ->modelClass )));
126+ $ relModelWords = Yii::t ('app ' , Inflector::camel2words (StringHelper::basename ($ AQ ->modelClass )));
123127 $ index ++;
124128 foreach ($ relModel ->errors as $ validation ) {
125129 foreach ($ validation as $ errorMsg ) {
@@ -152,7 +156,7 @@ public function saveAll($skippedRelations = [])
152156 }
153157 try {
154158 $ relModel ->deleteAll ($ query );
155- } catch (\ yii \ db \ IntegrityException $ exc ) {
159+ } catch (IntegrityException $ exc ) {
156160 $ this ->addError ($ name , "Data can't be deleted because it's still used by another data. " );
157161 $ error = true ;
158162 }
@@ -162,7 +166,7 @@ public function saveAll($skippedRelations = [])
162166 if (!empty ($ notDeletedPK )) {
163167 try {
164168 $ relModel ->deleteAll ($ query );
165- } catch (\ yii \ db \ IntegrityException $ exc ) {
169+ } catch (IntegrityException $ exc ) {
166170 $ this ->addError ($ name , "Data can't be deleted because it's still used by another data. " );
167171 $ error = true ;
168172 }
@@ -176,7 +180,7 @@ public function saveAll($skippedRelations = [])
176180 }
177181 $ relSave = $ records ->save ();
178182 if (!$ relSave || !empty ($ records ->errors )) {
179- $ recordsWords = \ Yii::t ('app ' , Inflector::camel2words (StringHelper::basename ($ AQ ->modelClass )));
183+ $ recordsWords = Yii::t ('app ' , Inflector::camel2words (StringHelper::basename ($ AQ ->modelClass )));
180184 foreach ($ records ->errors as $ validation ) {
181185 foreach ($ validation as $ errorMsg ) {
182186 $ this ->addError ($ name , "$ recordsWords : $ errorMsg " );
@@ -209,8 +213,8 @@ public function saveAll($skippedRelations = [])
209213 }
210214 try {
211215 $ relModel ->deleteAll (['and ' , $ condition ]);
212- } catch (\ yii \ db \ IntegrityException $ exc ) {
213- $ this ->addError ($ relData [$ relName ]['name ' ], \ Yii::t ('mtrelt ' , "Data can't be deleted because it's still used by another data. " ));
216+ } catch (IntegrityException $ exc ) {
217+ $ this ->addError ($ relData [$ relName ]['name ' ], Yii::t ('mtrelt ' , "Data can't be deleted because it's still used by another data. " ));
214218 $ error = true ;
215219 }
216220 } else {
@@ -220,8 +224,8 @@ public function saveAll($skippedRelations = [])
220224 }
221225 try {
222226 $ relModel ->deleteAll (['and ' , $ condition ]);
223- } catch (\ yii \ db \ IntegrityException $ exc ) {
224- $ this ->addError ($ relData [$ relName ]['name ' ], \ Yii::t ('mtrelt ' , "Data can't be deleted because it's still used by another data. " ));
227+ } catch (IntegrityException $ exc ) {
228+ $ this ->addError ($ relData [$ relName ]['name ' ], Yii::t ('mtrelt ' , "Data can't be deleted because it's still used by another data. " ));
225229 $ error = true ;
226230 }
227231 }
@@ -282,6 +286,7 @@ public function deleteWithRelated($skippedRelations = [])
282286 $ trans ->rollBack ();
283287 throw $ exc ;
284288 }
289+ return false ;
285290 }
286291
287292
@@ -310,7 +315,7 @@ public function getRelationData()
310315 }
311316 try {
312317 $ rel = call_user_func (array ($ this , $ method ->name ));
313- if ($ rel instanceof \ yii \ db \ ActiveQuery) {
318+ if ($ rel instanceof ActiveQuery) {
314319 $ name = lcfirst (preg_replace ('/^get/ ' , '' , $ method ->name ));
315320 $ stack [$ name ]['name ' ] = lcfirst (preg_replace ('/^get/ ' , '' , $ method ->name ));
316321 $ stack [$ name ]['method ' ] = $ method ->name ;
@@ -319,7 +324,7 @@ public function getRelationData()
319324 $ stack [$ name ]['link ' ] = $ rel ->link ;
320325 $ stack [$ name ]['via ' ] = $ rel ->via ;
321326 }
322- } catch (\ yii \ base \ ErrorException $ exc ) {
327+ } catch (ErrorException $ exc ) {
323328 //if method name can't be called,
324329 }
325330 }
@@ -374,8 +379,6 @@ public function getAttributesWithRelated()
374379 * source : https://github.com/kartik-v/yii2-krajee-base/blob/master/TranslationTrait.php
375380 * Edited by : Yohanes Candrajaya <[email protected] > 376381 *
377- * @param string $dir the directory path where translation files will exist
378- * @param string $cat the message category
379382 *
380383 * @return void
381384 */
0 commit comments