SS_HTTPRequest无法转换为字符串

时间:2016-09-22 13:07:16

标签: php forms silverstripe

我有一个SilverStripe(3.4)页面,如果我没有DataObject,那么该表单可以正常工作。

public function AntwortForm($ID) {

    $Nummer = Vortest_Fragen::get()->byID($ID);
    if ($Nummer) {
        $Art=$Nummer->Art;
    }
    if ($Art == 'Normal') {
        $fields = new FieldList(
            TextAreaField::create('Antwort'),
            HiddenField::create('ID', 'ID', $ID),
            HiddenField::create('VortestID', 'VortestID', $this->request->param('ID')),
            HiddenField::create('Aktion', 'Aktion', $this->request->param('Action'))

        );
    } else {
        $Optionen = explode(';', $Nummer->Optionen);
        $a = 'A';

        for ( $i = 0 ; $i < count ($Optionen); $i++) {
            $Op[$a] ='<div style="width:25px;display:inline;">' . $a . ')</div> ' . $Optionen[$i];
            $a++;
        }

        $fields = new FieldList(
            CheckboxSetField::create('Antwort', 'Antwort', $Op),
            HiddenField::create('ID', 'ID', $ID),
            HiddenField::create('VortestID', 'VortestID', $this->request->param('ID')),
            HiddenField::create('Aktion', 'Aktion',$this->request->param('Action')),
            HiddenField::create('Art', 'Art', $Nummer->Art)
        );
    }
    $actions = new FieldList(
        FormAction::create('AntwortEintragen', 'Eintragen')
    );

    $form = new Form($this, 'AntwortForm', $fields, $actions);
    return $form;
}

function AntwortEintragen($data, $form) {
    $Antwort = Vortest_Antwort::get()->filter(array('FrageID' => $data['ID'], 'SchreiberID' => Member::currentUserID()));
    foreach($Antwort as $item) {
        $item->delete();
    }

    foreach ($data['Antwort'] as $Antwort) {
        $Ant .= $Antwort . ',';
    }
    $Antwort = new Vortest_Antwort();

    if ($data['Antwort']) {
        $form->saveInto($Antwort);
        if ($data['Art'] == 'Mechanics') {
            $Antwort->Antwort = $Ant;
        }
        $Antwort->SchreiberID = Member::currentUserID();
        $Antwort->FrageID = $data['ID'];
        $Antwort->write();
    }
    $VID = $data['VortestID'];
    if ($data['Aktion'] == 'AlleFragen') {
        $this->redirect('/vortest/AlleFragen/' . $VID . '#' . $data['FrageNr']);
    } elseif ($data['Aktion'] == 'Einzelfrage') {
        $this->redirect('/vortest/Einzelfrage/' . $VID);
    } else {
        $this->redirect('/vortest/Test/' . $VID.'#' . $data['FrageNr']);
    }
}

当我将$ID更改为此行$Nummer = Vortest_Fragen::get()->byID($ID);

中的数字时,它可以正常工作

当我不更改它时,我收到以下错误:

[Recoverable Error] Object of class SS_HTTPRequest could not be converted to string

如何解决此问题?

1 个答案:

答案 0 :(得分:0)

虽然没有明确记录,但Silverstripe秘密地将一个请求参数传递给控制器​​并在控制器上形成方法。您的$ ID参数实际上并不是您认为的,您会发现它实际上是Silverstripe已经通过的SS_HTTPRequest对象(没有您意识到)。

要解决此问题,请更改第一行:

public function AntwortForm($ID) {

public function AntwortForm($request, $ID) {

并确保在您调用此方法的任何地方更新: - )