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.

General Questions /

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

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

[SOLVED] Add Object by User Forntend


Go to End
Reply


4 Posts   755 Views

Avatar
Craftnet

Community Member, 58 Posts

15 February 2012 at 6:43pm

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
Avatar
martimiz

Forum Moderator, 1201 Posts

16 February 2012 at 12:14am

Edited: 16/02/2012 12:14am

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 ;

	}

Avatar
Craftnet

Community Member, 58 Posts

16 February 2012 at 2:23pm

Edited: 16/02/2012 8:41pm

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

Avatar
martimiz

Forum Moderator, 1201 Posts

16 February 2012 at 11:32pm

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