Jump to:

17452 Posts in 4473 Topics by 1971 members

Archive

SilverStripe Forums » Archive » Links Section on Website

Our old forums are still available as a read-only archive.

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

Page: 1 2 3
Go to End
Author Topic: 5299 Views
  • gakenny
    Avatar
    Community Member
    153 Posts

    Re: Links Section on Website Link to this post

    Thanks Will...

    Anyone have any ideas on how to update the counter? I am struggling with the DB update...

  • Willr
    Avatar
    Forum Moderator
    5482 Posts

    Re: Links Section on Website Link to this post

    I dont see why $Link->update() doesnt work.

    I would throw a Debug::show() in the function to see whats going on!

    ..
    $link->Counter++;
    $link->update(); // pretty sure this works
    Debug::show($link); // is the counter updated!?!?!?!
    die(); // dont redirect so you can see the debug statement

    return Director::redirectTo($link->Link);

  • gakenny
    Avatar
    Community Member
    153 Posts

    Re: Links Section on Website Link to this post

    Hello Will,

    False alarm! It appears that the database field was updated - it just wasn't displaying in the backend. Why would the counter not be updated when selecting the relevant LinkPage when logged in as Administrator.

    Anyway, here is my solution:

    In my link list, add this code:
    <li><a href="{$Link}go" title="Read more on &quot;{$Title}&quot;">$Title</a></li>

    In the LinkPage type controller:
    function go() {
    $link = DataObject::get_by_id("LinkPage", $this->ID);
    $link->Counter++;
    $link->write();
    return Director::redirect("$link->URL");
    }

    Cheers,

    Gary

  • Willr
    Avatar
    Forum Moderator
    5482 Posts

    Re: Links Section on Website Link to this post

    Does that actually work?

    <a href="{$Link}go"...

    Wouldnt that point to like

    <a href="www.google.comgo" ?

    The link would need to be something like

    <a href="links/go?=$Link">

    as that would point to the go method on the links Page?

  • gakenny
    Avatar
    Community Member
    153 Posts

    Re: Links Section on Website Link to this post

    Hi Will,

    Heh heh - It sure does work! $Link contains the URL to the relevant LinkPage that has previously been defined. It seems that this already has the slash ('/') added as "$Link/go" contains two slashes.

    Because, I am linking to the relevant LinkPage, I did not need to supply the ID as a parameter to the function and instead used the $this->ID variable within the function itself.

    I could PM the site to you if you want to have a look - it is still work in progress.

    Cheers,

    Gary

  • Willr
    Avatar
    Forum Moderator
    5482 Posts

    Re: Links Section on Website Link to this post

    Ok so if the DB is being updated? and its not displaying the new value in the CMS?.. Tried flushing the cms - admin?flush=1 as if thats pulling from the DB and the DB has the correct value somethings not right is a link a DataObject?

  • simon_w
    Avatar
    Forum Moderator
    471 Posts

    Re: Links Section on Website Link to this post

    I had a similar problem with the kudos widget. This was because the live version of the object was being updated, but not the stage which is shown in the database.

    The code I used to fix:

          $this->value++;
          $this->writeToStage("Stage");
          // Make it think that we've changed the variable so the Live site gets updated as well
          $this->value++;
          $this->value--;
          $this->write();

  • gakenny
    Avatar
    Community Member
    153 Posts

    Re: Links Section on Website Link to this post

    Hi Will,

    By dataobject do you mean have I defined a page type called LinkPage and defined URL and Counter as additional fields.

    I have removed the Counter from being displayed in the CMS and have instead shown this in the LinkHolder when displaying all links (and sorting by Counter).

    The database is being updated using the code shown earlier and it is incrementing at each link click.

    Cheers,

    Gary

    5299 Views
Page: 1 2 3
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.