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

Saving the Author


Go to End


3 Posts   1168 Views

Avatar
Parker1090

Community Member, 46 Posts

1 February 2013 at 11:36pm

Hello!
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() {
		parent::onBeforeWrite();
		
		if(!$this->ID) {
			$currentMember = Member::currentMember();
			if($currentMember) {
				$this->AuthorId = $currentMember->ID;
			}
		}
		
	}

Any help would be massively appreciated :)

Avatar
Willr

Forum Moderator, 5523 Posts

4 February 2013 at 8:03pm

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

Avatar
Parker1090

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!