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.

Archive /

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

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

Links Section on Website


Go to End


17 Posts   6392 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, 5523 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, 5523 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, 5523 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
(deleted)

Community Member, 473 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