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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Customising the CMS /

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

Saving the Author

Go to End

3 Posts   869 Views


Community Member, 46 Posts

1 February 2013 at 11:36pm

I've just done the tutorial for the simple news/blog listings, which is great for what I need. However, instead of putting in a date field, I've used the Created field in the database (which is fine for what I need), but I'm looking to automate the authors name too.

What I'm trying to do, is save the author's ID with a news post, and then when the page is called, pick up the authors name too (so you don't have to input it when you set up the page). I've had a go through $has_one ('Author' => 'Member'), which created an AuthorID field, but I have no idea how to save this. I've tried the following (based on examples I've found online and in the book), but it doesn't work and always saves the value of 0:

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

Any help would be massively appreciated :)


Forum Moderator, 5523 Posts

4 February 2013 at 8:03pm

It may be case sensitive (e.g AuthorID rather than AuthorId).


Community Member, 46 Posts

9 February 2013 at 5:01am

Wow! Don't I feel stupid. Thanks - that worked.

As the two tables are linked, when pulling data (say, for arguments sake), the home page - again based on the code in the tutorial, the Author can be accessed like this:

<p><strong>Date Posted</strong>: $Created.Long, by 
					<% control Author %>$FirstName $Surname<% end_control %>

However, when trying to do it on the article page itself, it returns blank. Any suggestions o this one?

Thanks in advance!