2016-10-25 87 views
1

我有一個結構的表像這樣Yii2更新親子

enter image description here

我想改變記錄數據字段GOID = 1,但我因爲你可以看到我的代碼波紋管,有限制循環。有沒有更好的方法來做到這一點?

public function actionShareGoid($folder) 
{ 
    $one = DokumenFolder::findOne($folder); 
    $one->goid = '1'; 
    $one->update(false); 

    $models_1 = DokumenFolder::find()->where(['parent_id'=>$one->id])->all(); 
    foreach ($models_1 as $key_1 => $model_1) { 
     $model_1->goid = '1'; 
     $model_1->update(false); 
     $models_2 = DokumenFolder::find()->where(['parent_id'=>$model_1->id])->all(); 
     foreach ($models_2 as $key_2 => $model_2) { 
      $model_2->goid = '1'; 
      $model_2->update(false); 
      $models_3 = DokumenFolder::find()->where(['parent_id'=>$model_2->id])->all(); 
      foreach ($models_3 as $key_3 => $model_3) { 
       $model_3->goid = '1'; 
       $model_3->update(false); 
       $models_4 = DokumenFolder::find()->where(['parent_id'=>$model_3->id])->all(); 
       foreach ($models_4 as $key_4 => $model_4) { 
        $model_4->goid = '1'; 
        $model_4->update(false); 
       } 
      } 
     } 
    } 

    return $this->redirect(Yii::$app->request->referrer); 
} 

回答

1

感謝某人,我得到了答案。^_^

所以這裏...

public function folderParent($id) 
{ 
    $models = DokumenFolder::find()->where(['parent_id'=>$id])->all(); 
    foreach ($models as $key => $model) { 
     $model->goid = '1'; 
     $model->update(false); 
    } 
} 

public function actionShareGoid($id) 
{ 
    $models = DokumenFolder::find()->where(['parent_id'=>$id])->all(); 
    foreach ($models as $key => $model) { 
     $this->folderParent($model->id); 
    } 
    return $this->redirect(Yii::$app->request->referrer); 
}