无法找到数据写入程序所需的现有数据

时间:2015-10-16 23:15:01

标签: xenforo

在我的加载项页面上,除非我尝试更新现有行,否则创建效果非常好。 `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('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()
        );

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

My Addon page

1 个答案:

答案 0 :(得分: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,以便您不保存访客信息。 (除非那是你想要的)