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.

Archive /

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

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

Links Section on Website


Reply


17 Posts   5505 Views

Avatar
gakenny

Community Member, 153 Posts

16 June 2008 at 5:36pm

Thanks Will...

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

Avatar
Willr

Forum Moderator, 5513 Posts

16 June 2008 at 5:49pm

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);

Avatar
gakenny

Community Member, 153 Posts

16 June 2008 at 7:20pm

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

Avatar
Willr

Forum Moderator, 5513 Posts

16 June 2008 at 7:52pm

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?

Avatar
gakenny

Community Member, 153 Posts

16 June 2008 at 8:06pm

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

Avatar
Willr

Forum Moderator, 5513 Posts

16 June 2008 at 8:12pm

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 :P is a link a DataObject?

Avatar
simon_w

Forum Moderator, 474 Posts

16 June 2008 at 8:21pm

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();

Avatar
gakenny

Community Member, 153 Posts

16 June 2008 at 8:24pm

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