Silverstripe:成员关系很多很多null,形成前端

时间:2017-10-02 15:44:57

标签: php forms silverstripe member

致命错误:在null上调用成员函数Services()

class MemberProfileViewer extends Page_Controller {

    // ...

    public function Servizi() {
        $source = null;
        if ($id = (int)$this->urlParams['ID']) {
            $member = Member::get()->byID($id);
            if ($member instanceof Member) {
                return $source = $member->Services()->map('Title','Title');
            }
        } 

        if (is_null($id)) {
            return $this->redirect('/');
        }

        if (is_null($source)) {
            return $this->httpError(404);
        } 

    }

    public function form() {

        $fields = new FieldList(
            CheckboxSetField::create(
                $name = "ServiceID",
                $title = "Select",
                $source = $this->Servizi()
            ),
        )

        $actions = FieldList::create(
                FormAction::create('form', 'Send')
                    ->setUseButtonTag(true)
                    ->addExtraClass('btn btn-primary')
        );

        $validator = new RequiredFields('ServiceID');

        $form = new Form($this, 'form', $fields, $actions, $validator);
        $form->setFormMethod('POST', true);
        return $form;
    }

    public function doform($data, $form) {
        $form->sessionMessage(
            "Thank you, you will be contacted as soon as possible by our consultant.", 'good'
        );

        $submission = new RequestForm();
        $form->saveInto($submission);
        $submission->write();

        return $this->redirectBack();

    }

}

  

始终为空
我不知道如何解决

是的,这解决了这个问题!

class MemberProfileViewer extends Page_Controller {

    // ...

    public function currentPageID() {
        $id = 0;
        $request = $this->getRequest();
        if(is_numeric($request->requestVar('ID')))  {
            $id = $request->requestVar('ID');
        } elseif (is_numeric($request->param('ID'))) {
            $id = $request->param('ID');
        }

        $id = (int)$id;
        return $id;
    }

    public function Servizi() {
        $source = null;
        if ($id = $this->currentPageID()) {
            Session::set('ID', $id);
            return $source = Member::get()->byID($id)->Services()->map('Title','Title')->toArray();
        } elseif (is_null($source)) {
           return $source = Member::get()->byID(Session::get('ID'))->Services()->map('Title','Title')->toArray();
        } 

    }

    public function form() {

        $fields = new FieldList(
            CheckboxSetField::create(
                $name = "Service",
                $title = "Select",
                $source = $this->Servizi()
            ),
        )

        $actions = FieldList::create(
                FormAction::create('form', 'Send')
                    ->setUseButtonTag(true)
                    ->addExtraClass('btn btn-primary')
        );

        $validator = new RequiredFields('Service');

        $form = new Form($this, 'form', $fields, $actions, $validator);
        $form->setFormMethod('POST', true);

        $data = Session::get("FormData.{$form->getName()}.data");
        return $data ? $form->loadDataFrom($data) : $form;
    }

    public function doform($data, $form) {
        Session::set("FormData.{$form->getName()}.data", $data);

        $submission = new RequestForm();
        $form->saveInto($submission);
        $submission->write();

        $form->sessionMessage(
            "Thank you, you will be contacted as soon as possible by our consultant.", 'good'
        );

        Session::clear("FormData.{$form->getName()}.data");
        return $this->redirectBack();

    }

}

2 个答案:

答案 0 :(得分:0)

我愿意:

$source = null;
if ($id = $this->urlParams['ID']) {
    $member = Member::get()->byId($id);
    if ($member instanceof Member) {
        $source = $member->Services()->map('Title','Title');
    }
}

if (is_null($source)) {
    //don't show that field, show a LiteralField with an error maybe
} else {
    //show your field with $source
}

答案 1 :(得分:0)

你的问题可能是因为这行

<div class="media_field_1">    
  Image <%= f.text_field :image_url, class: 'form-control' %>
</div>
<div class="media_field_2"> 
  Video <%= f.text_field :video_url, class: 'form-control' %>
</div> 

您的方法$fields = new FieldList( CheckboxSetField::create( $name = "ServiceID", $title = "Select", $source = $this->Servizi() // <-- ), ) 取决于某些路由(您尝试从URL获取成员ID)。该方法不会始终为CheckboxSetField返回一个好的地图。如果您没有Servizi()方法的路线,则可能不想重定向或返回http错误。

现在,我不知道您是如何获得会员ID的,这取决于您如何获得会员ID。但是你可以像这样解决它。

Servizi()

上述代码未经过测试。