Jump to:

3372 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » [SOLVED] Frontend - not save Member user as owner

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

Page: 1
Go to End
Author Topic: 391 Views
  • Craftnet
    Avatar
    Community Member
    58 Posts

    [SOLVED] Frontend - not save Member user as owner Link to this post

    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

  • Craftnet
    Avatar
    Community Member
    58 Posts

    Re: [SOLVED] Frontend - not save Member user as owner Link to this post

    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;
    }
    }

    }

    391 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.