Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Data Model Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

[SOLVED] Frontend - not save Member user as owner


Go to End


2 Posts   1277 Views

Avatar
Craftnet

Community Member, 58 Posts

22 December 2012 at 11:00am

Edited: 22/12/2012 2:29pm

Hi,
I have DataObject (Obiekt) + form to add this obiect from frontend (SS 3.1-beta).
User must be loggin.

User fill everything and add obiekt but he don't save as owner this object.
Member is has_manyObiekt
Obiekt is has_one Member

OBIEKT.PHP

<?php

class Obiekt extends DataObject {

    static $db = array(
        ....
    );

   
    static $has_one = array(
       'Member' => 'Member'
        .....
    );

    static $summary_fields = array(
        'Member.FirstName' => 'Imię właściciela',
        'Member.Surname' => 'Nazwisko właściciela'
....
    );

    //Add an SQL index for the URLSegment
    static $indexes = array(
        "URLSegment" => true
    );

    static $searchable_fields = array (
       ....
    );

    public function getCMSFields() {
        $fields = parent::getCMSFields();

        $member_map = Member::get()->map();


        //MAIN TAB
        $fields->addFieldsToTab("Root.Main", array(
            new DropdownField('MemberID', 'Owner', $member_map )
        ));

        ....

        return $fields;
    }

    function onBeforeWrite() {
        if((!$this->URLSegment || $this->URLSegment == 'nowy-obiekt') && $this->Title != 'Nowy Obiekt' && $this->Obiekty != '1') {
            $this->URLSegment = Convert::raw2url($this->Title);
        }
        else if($this->isChanged('URLSegment')) {
            $segment = preg_replace('/[^A-Za-z0-9]+/','-',$this->URLSegment);
            $segment = preg_replace('/-+/','-',$segment);

            if(!$segment) {
                $segment = "obiekt-$this->ID";
            }
            $this->URLSegment = $segment;
        }

        $count = 2;
        while($this->LookForExistingURLSegment($this->URLSegment)) {
            $this->URLSegment = preg_replace('/-[0-9]+$/', null, $this->URLSegment) . '-' . $count;
            $count++;
        }

        //-----------------------------------------

        if((!$this->MetaTitle || $this->MetaTitle == 'nowy obiekt') && $this->Title != 'Nowy Obiekt') {
            $this->MetaTitle = $this->Title;
        }
        parent::onBeforeWrite();
    }
}

AddObiektPage.PHP

<?php

class AddObiektPage extends Page{}

class AddObiektPage_Controller extends Page_Controller {

    //Allow our form as an action
    static $allowed_actions = array(
        'ObiektForm'
    );

    //Generate the form
    function ObiektForm() {

        $fields = singleton('Obiekt')->getFrontendFields();
        $fields->push(new HiddenField('MemberID', 'Właściciel'));
        ....

        // Create action
        $validator = new RequiredFields(
        //staff
        );

        // Create action
        $actions = new FieldList(
            new FormAction('doAdd', 'Dodaj obiekt')
        );

        return new Form($this, 'ObiektForm', $fields, $actions, $validator);

    }


    function doAdd($data,$form)
    {
        $member = Member::currentUser();

        if($Imie = DataObject::get_one("Obiekt", "`Title` = '". Convert::raw2sql($data['Title']) . "'"))
        {
            //Set error message
            $form->AddErrorMessage('Title', "<div class='alert alert-warning'>Nazwa obiektu jest już zajęta. Proszę wybrać inną lub przedłużyć obecny obiekt</div>", 'bad');
            //Set form data from submitted values
            Session::set("FormInfo.Form_Obieikt.data", $data);
            //Return back to form
            return Director::redirectBack();
        }

        $Obiekt = new Obiekt();
        $form->saveInto($Obiekt);
        $Obiekt->write();

        //Link do faktur
        $ProfilePage = DataObject::get_one('EditAccountPage');
        $Link = $ProfilePage->RelativeLink('platnosci');

        if($Obiekt) {
            $this->setMessage('warning', 'Obiekt pomyślnie został dodany, jednak nie będzie on widoczny do czasu jego <a href="' . $Link .  '">opłacenia</a>');
            return Controller::curr()->redirect('/' . 'obiekty');
        }
        else {
            $this->setMessage('error', 'Obiekt nie został dodany');
            return Controller::curr()->redirect('/' . 'obiekty');
        }

    }

}

Any Idea?

Sorry for my bad English

Avatar
Craftnet

Community Member, 58 Posts

22 December 2012 at 2:26pm

OK, I'm solved

in Object.php in function onBeforeWrite() I was must add

function onBeforeWrite() {

        ....

        parent::onBeforeWrite();

        if(!$this->ID) {
            $currentMember = Member::currentUser();
            if($currentMember) {
                $this->MemberID = $currentMember->ID;
            }
        }

    }