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