Jump to:

22979 Posts in 11760 Topics by 2827 members

General Questions

SilverStripe Forums » General Questions » [SOLVED] Add Object by User Forntend

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

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

    [SOLVED] Add Object by User Forntend Link to this post

    Hi,
    I need help.
    Sorry for my bad English

    I have site where when user regitering he could add Object do DataObject (by Frontend)
    And here is the problem.
    I want know who add Object - i must write to base current login user.

    Generally I would do to the object added by the user was assigned to him

    I have:

    Page to Add Object

    class AddOgloszeniePage extends Page
    {

    }

    class AddOgloszeniePage_Controller extends Page_Controller
    {
       //Allow our form as an action
       static $allowed_actions = array(
          'OgloszenieForm'
       );
       
       //Generate the ogloszenia form
       function OgloszenieForm()
       {
       
             $fields = singleton('Ogloszenie')->getFrontendFields();
             $fields->removeByName('Member');
             

       
           
        // Create action
        $actions = new FieldSet(
             new FormAction('doAdd', 'Add')
        );
          // Create action
          $validator = new RequiredFields('Price');
          
           return new Form($this, 'OgloszenieForm', $fields, $actions, $validator);      

       }

       function doAdd($data,$form)
       {   
          

          $Ogloszenie = new Ogloszenie();
          $form->saveInto($Ogloszenie);
          $Ogloszenie->write();
          
       }

       
       function Link() {
          return $this->URLSegment;
       }
       
       
    }

    My DataObject

    <?php

    class Ogloszenie extends DataObject
    {
       static $db = array(
          'Title' => 'Varchar(255)',
          'Description' => 'HTMLText',
          'Price' => 'Decimal(6,2)',
          'URLSegment' => 'Varchar(255)',
          'MetaTitle' => 'Varchar(255)',
       );

       //Set our defaults
       static $defaults = array(   
          'Title' => 'New Ogloszenie',
          'URLSegment' => 'new-ogloszenie'
       );
       
       static $has_one = array(
          'Image' => 'Image',
          'Member' => 'Member'
       );
       

       .......

       function getCMSFields()
       {
          $fields = parent::getCMSFields();
          if($result = DataObject::get("Member")) {
       $member_map = $result->toDropdownMap();
    }
          //Main Tab
          $fields->addFieldToTab("Root.Main", new TextField('Title', 'Title'));   
          $fields->addFieldToTab("Root.Main", new TextField('URLSegment', 'URL Segment'));   
          $fields->addFieldToTab("Root.Main", new TextField('MetaTitle', 'Meta Title'));   
          $fields->addFieldToTab("Root.Main", new NumericField('Price'));            
          $fields->addFieldToTab("Root.Main", new HTMLEditorField('Description'));
          $fields->addFieldToTab("Root.Main", new DropdownField('MemberID', 'Właściciel', $member_map ));
          
          //Categories
          $Categories = DataObject::get('CategoryPage');
          $fields->addFieldToTab("Root.Categories", new CheckboxsetField('Categories', 'Categories', $Categories));
       
          //Images
          $fields->addFieldToTab("Root.Images", new ImageField('Image', 'Image', Null, Null, Null, 'Uploads/category_banners'));
       
          return $fields;
       }

          
       ......
       
    }

    And in Member DataObjectDecoration

    <?php
    class MemberDecorator extends DataObjectDecorator {

       //Add extra database fields
       public function extraStatics()
       {   
          return array(
             'db' => array(
                ....
             ),
             'has_many' => array(
             'Ogloszenia' => 'Ogloszenie'
             )
          );
       }
       
       ......
       
    }

    I think I need to combine at this point but I have no more ideas

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

    One more time, sorry for my bad English

    Attached Files
  • martimiz
    Avatar
    Forum Moderator
    1037 Posts

    Re: [SOLVED] Add Object by User Forntend Link to this post

    Something like this should probably work. In your object:

    function OnBeforeWrite() {
       parent::OnBeforeWrite();
          
       if( $member = Member::currentUser() ) {
          $this->Member = $member;

       }
    }

    or lighter:

       if( $memberID = Member::currentUserID() ) { {
          $this->MemberID = $memberID ;

       }

  • Craftnet
    Avatar
    Community Member
    58 Posts

    Re: [SOLVED] Add Object by User Forntend Link to this post

    Thx for replay.

    It works great except one thing
    When I add by user object in database save good, but when i go in backend end edit this by administrator and save the DataObject save owner as Administrator

    I understand why this happens (When i edit by admistrator in backend function onBeforeWrite save me as owner) but i don't know how to solve this problem.

    Sorry for my bad English.

    Once again, many thanks for your help

    EDIT: SOLVED PROBLEM

    function OnBeforeWrite() {
    parent::onBeforeWrite();
          
          if(!$this->ID) {
             $currentMember = Member::currentMember();
          if($currentMember) {
             $this->MemberID = $currentMember->ID;
          }
    }

    Once again, many thanks for your help without your help I would not cope

  • martimiz
    Avatar
    Forum Moderator
    1037 Posts

    Re: [SOLVED] Add Object by User Forntend Link to this post

    Right - I didn't take into account someone else editing the record afterwards. Glad you got that solved then

    462 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.