0

在我的插件页面上,除非我尝试更新现有行,否则我正在创建的插件效果很好。`公共函数actionUpdate(){

$visitor = XenForo_Visitor::getInstance();
     $userName = $visitor['username'];
//Get the text that user wrote in the text box
$text3 = $this->_input->filterSingle('simple_text2', XenForo_Input::STRING);
$text4 = $this->_input->filterSingle('simple_text3', XenForo_Input::STRING);

//Create a instance of our DataWriter
$dwSimpleText = XenForo_DataWriter::create('MinecraftAdder_DataWriter_MinecraftAdder');

//Set the field with the data we filtered
$dwSimpleText->setExistingData('Name');
$dwSimpleText->set('Name', $text3);
$dwSimpleText->setExistingData('Rank');
$dwSimpleText->set('Rank', XenForo_Visitor::getUserId());
$dwSimpleText->setExistingData('UUID');
$dwSimpleText->set('UUID', $text4);
$dwSimpleText->setExistingData('UserID');
$dwSimpleText->set('UserID', $userName);
//Save in the database, please!
$dwSimpleText->save();

//Send a response to the user, so he know that everything went fine with this action
return $this->responseRedirect(
            XenForo_ControllerResponse_Redirect::SUCCESS,
            $this->getDynamicRedirect()
        );

}` 我收到错误找不到数据编写器所需的现有数据。有谁知道如何解决这一问题?

我的插件页面

4

1 回答 1

0

你的用法setExistingData不正确。该函数必须被赋予以下两个值之一:

  • 要更新的行的主键列的值
  • 包含要更新的行的所有数据的数组

因此,在您的情况下,由于您没有提前选择该行,因此您将使用选项 1。假设UserID是您的主键列,您的代码将是:

public function actionUpdate() {
    $visitor = XenForo_Visitor::getInstance();
    $userName = $visitor['username'];
    //Get the text that user wrote in the text box
    $text3 = $this->_input->filterSingle('simple_text2', XenForo_Input::STRING);
    $text4 = $this->_input->filterSingle('simple_text3', XenForo_Input::STRING);

    //Create a instance of our DataWriter
    $dwSimpleText = XenForo_DataWriter::create('MinecraftAdder_DataWriter_MinecraftAdder');

    //Set the field with the data we filtered
    $dwSimpleText->setExistingData($userName);
    $dwSimpleText->set('Name', $text3);
    $dwSimpleText->set('Rank', XenForo_Visitor::getUserId());
    $dwSimpleText->set('UUID', $text4);
    //Save in the database, please!
    $dwSimpleText->save();

    //Send a response to the user, so he know that everything went fine with this action
    return $this->responseRedirect(
        XenForo_ControllerResponse_Redirect::SUCCESS,
        $this->getDynamicRedirect()
    );
}

其他一些提示:

  • 如果actionUpdate只能通过表单访问,则应$this->_assertPostOnly();在函数顶部添加以确保请求是 POST 请求。
  • 您可能需要检查访客是否user_id存在0,这样您就不会保存客人的信息。(当然,除非那是你想要的)
于 2015-10-20T10:16:05.883 回答