Jump to:

5450 Posts in 1672 Topics by 1197 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Saving the Author

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

Page: 1
Go to End
Author Topic: 338 Views
  • Parker1090
    Avatar
    Community Member
    38 Posts

    Saving the Author Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Saving the Author Link to this post

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

  • Parker1090
    Avatar
    Community Member
    38 Posts

    Re: Saving the Author Link to this post

    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!

    338 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.