请求页面的操作删除不存在

时间:2016-02-18 18:31:55

标签: php view yii2

您好我要删除一些数据。我确实查看了我想删除附加数据的位置。这是我的控制器veiew因为我想在那里按钮我可以删除数据:

 public function actionView($id) {

        return $this->render('view', [
                    'model' => $this->findModel($id),
                    'userDate'=>$this->findData($id)
        ]);
    }


 public function actionDelet($id) {

    $this->findData($id)->delete();

    return $this->redirect(['index']);
}

public function findData($id){
     if (($model = Profile::findOne($id)) !== null) {
         $id=$model->Rel_UserData;
         $user = UserData::find()->where(['Id' => $id])->one();

        return $user;
    } else {
        throw new NotFoundHttpException('The requested page does not st.');
    }

}

2 个答案:

答案 0 :(得分:0)

我猜解决方案是这样的:

$item = $this->findData($id);
$item->delete();

答案 1 :(得分:0)

您已经有一个调用UrRoyalUserData类的关系函数:getRelRoyalUserData()。您可以简化代码:

public function findData($id)
{
    if (($model = Profile::findOne($id)) !== null) {
        $user = $model->relRoyalUserData;

        return $user;
    }

    throw new NotFoundHttpException('The requested page does not st.');   
}

除此之外,您可以更改行动并检查返回的内容吗?

public function actionDelet($id)
{    
    var_dump($this->findData($id));
}

如果它引发了同样的错误,那意味着你的表中没有Profile。但是,如果不返回UrRoyalUserData类,则问题是您没有与UrRoyalUserData相关的任何Profile