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.

Customising the CMS /

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

Save author in CMS


Go to End


5 Posts   1645 Views

Avatar
Bereusei

Community Member, 96 Posts

7 May 2011 at 6:57pm

Hello guys,

I´m working on an simple form with which users can save information. The form works pretty good, but I also want to save the name of the author (currentMember) from these entry.

I´ve found some code that work with an onBeforeWrite-function which gets the author id. But I can´t figure out how I can use this to save the author name in cms.

Here´s my code:

--------------------Meldung.php--------------------

class Meldung extends DataObject{
static $db = array(
'Datum' => 'Date',
'Ausrichter' => 'Text',
'Klasse' => 'Text',
'gemeldet' => 'Boolean'
);

static $has_one = array(
'Meldeliste' => 'Meldeliste',
'Author' => 'Member'
);

function getCMSFields(){
$fields = new FieldSet(
new DateField('Datum', 'Datum'),
new TextField('Ausrichter', 'Ausrichter'),
new TextField('Klasse', 'Klasse'),
new CheckboxField('gemeldet', 'gemeldet')
);
return $fields;
}

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

--------------------Meldeliste.php--------------------

class Meldeliste extends Page{
static $db = array();

static $has_many = array(
'Meldeliste' => 'Meldung'
);

function getCMSFields(){
$fields = parent::getCMSFields();
$meldeTabelle = new ComplexTableField(
$this,
'Meldeliste',
'Meldung',
null,
null,
"Meldung.MeldelisteID = {$this->ID}"
);
$fields -> addFieldtoTab('Root.Content.Meldeliste', $meldeTabelle);
return $fields;
}
}

class Meldeliste_Controller extends Page_Controller{
function Form(){
$fields = new FieldSet(
new DateField('Datum', 'Datum'),
new TextField('Ausrichter', 'Ausrichter'),
new TextField('Klasse', 'Klasse')
);

$actions = new FieldSet(
new FormAction('doSubmitEntry', 'Turnier melden')
);

$validator = new RequiredFields(
'Datum',
'Ausrichter',
'Klasse'
);

$form = new Form(
$this,
'Form',
$fields,
$actions,
$validator
);
return $form;
}

function doSubmitEntry($data, $form){
$Meldung = new Meldung();
$form->saveInto($Meldung);
$Meldung->MeldelisteID=$this->dataRecord->ID;
$Meldung->write();
$form->sessionMessage('Turnier erfolgreich gemeldet', 'good');
Director::redirectBack();
return;
}
}

Some words are German, I hope it doesn´t matter.

Greetings,
Sascha

Avatar
swaiba

Forum Moderator, 1899 Posts

7 May 2011 at 9:50pm

Hi Bereusei,

Once you've saved the Member, that means you have access to the full member details - how exactly do you want to use it?
Show it in the CMSFields?

Avatar
Bereusei

Community Member, 96 Posts

8 May 2011 at 12:09am

If you click in the CMS (in the Backend) on the tab "Meldeliste", you get an table with all entries from "Meldeliste". I want to see in this table the name of the author (currentMember).
I also want to get the name of the author in an table at the frontend.

Here the code from ss:
<table border="0" cellpadding="0" cellspacing="10" width="100%">
<tr>
<td class="bold">Datum</td>
<td class="bold">Ausrichter</td>
<td class="bold">Klasse</td>
<td class="bold">Gemeldet</td>
</tr>
<% control Meldeliste %>
<tr id="Meldung{$ID}">
<td>$Datum.format(d.m.Y)</td>
<td>$Ausrichter</td>
<td>$Klasse</td>
<td>$gemeldet</td>
</tr>
<% end_control %>
</table>

I´ve added an image to show what I mean. Does anybody know why I only see the ID and not the other datas (Datum, Ausrichter, Klasse, etc)? If I click on the magnifying glass everything looks fine and I can see all informations.

I hope it´s clear what I mean. Thanks for help.

Avatar
swaiba

Forum Moderator, 1899 Posts

8 May 2011 at 5:04am

Yep, that is very clear, you need to specficy static $summary_fields, try reading up on this...

http://doc.silverstripe.org/sapphire/en/reference/dataobject#summary-fields

Avatar
Bereusei

Community Member, 96 Posts

9 May 2011 at 6:54pm

Thanks for the tip swaiba, you got me on the right way. I can work with it further.

Just for info, I modify my code to this:
<?php
$fields = singleton('Meldung')->summaryFields();

class Meldung extends DataObject{
static $db = array(
'Datum' => 'Date',
'Ausrichter' => 'Text',
'Klasse' => 'Text',
'Author' => 'Text',
'gemeldet' => 'Boolean'
);

static $has_one = array(
'Meldeliste' => 'Meldeliste',
'Author' => 'Member'
);

static $summary_fields = array(
'Author.Name',
'Datum' => 'Date',
'Ausrichter' => 'Text',
'Klasse' => 'Text'
);

static $default_sort = "Datum ASC";

function getCMSFields_forPopup(){
$fields = new FieldSet(
new DateField('Datum', 'Datum'),
new TextField('Ausrichter', 'Ausrichter'),
new TextField('Klasse', 'Klasse'),
new CheckboxField('gemeldet', 'gemeldet')
);
return $fields;
}

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