Jump to:

23376 Posts in 18295 Topics by 2867 members

General Questions

SilverStripe Forums » General Questions » News Articles read by members.

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

Page: 1
Go to End
Author Topic: 515 Views
  • micahsheets
    Avatar
    Community Member
    164 Posts

    News Articles read by members. Link to this post

    I need a way to list a range of news articles so that articles a logged in member has not read show up bold and ones they have read are not bold.

    I suppose that this can be done with some sort of database relationship, but I am not sure the best way to do this.

    I was thinking of making the News Articles have a static $belongs_many_many relationship to the members but I am not sure how I would set this up so that when a member reads an article it gets put in the belongs_many_many table.

    But that is just a guess.

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: News Articles read by members. Link to this post

    You'd modify the "index" method of your article controller. You'd also want a 'has current member read this' method. In this example, I'm assuming that the relation is called "MembersRead" Eg:

    function index() {
       if(Member::currentUser() && !$this->HasBeenRead()) {
          $components = $this->MembersRead();
          $components->add(Member::currentUser());
          $components->write();
       }
    }

    function HasBeenRead() {
       if(Member::currentUser())
          return in_array(array_keys(Member::currentUser()->ID, $this->MembersRead()->getIdList()));         
       else
          return false;
    }

    This is untested, but should give you a head start.

  • micahsheets
    Avatar
    Community Member
    164 Posts

    Re: News Articles read by members. Link to this post

    @Hamish,

    Thanks for the tip. I was wondering what the index() function is specifically. It seems that it is run when the controller is instantiated just like init() but it must be different.

    515 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.