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.

Data Model Questions /

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

Linking to a page based on page ID


Go to End


10 Posts   3401 Views

Avatar
ipv89

Community Member, 6 Posts

9 November 2016 at 7:51am

I have implemented a page counter that stores page hits in its own table and records the page ID.

I have written a function that returns the top 5 pages based on views but I only have the page ID's to work with.

How can I get the link to a page based on the page id?

Avatar
blackduck

Community Member, 13 Posts

16 November 2016 at 3:48pm

How about using dataobject's get_by_id to return each page then pull the link from that.
http://api.silverstripe.org/3.4/class-DataObject.html#_get_by_id

Avatar
ipv89

Community Member, 6 Posts

23 November 2016 at 11:55am

I have managed to get the data object using

$dataO = DataObject::get("Page", "`ID` = '$id'");

However I can still not access the link, I though it might be $dataO.Link or $dataO->$Link ?

Avatar
blackduck

Community Member, 13 Posts

23 November 2016 at 12:20pm

Link is a method of SiteTree
$dataO->$Link()

http://api.silverstripe.org/3.4/class-SiteTree.html#_Link

Avatar
ipv89

Community Member, 6 Posts

23 November 2016 at 1:07pm

Using that gives an undefined variable error. I can see how the functions are inherited but I must still be missing something.

Avatar
blackduck

Community Member, 13 Posts

23 November 2016 at 2:20pm

I just tried this and get the same undefined variable error.
$dataO = DataObject::get_by_id("Page", $id);
return $dataO->Link();

but this does work and I'd expect them to both work.
$dataO = DataObject::get_by_id("Page", $id);
return $dataO->AbsoluteLink();
I'd probably stick with
$dataO = DataObject::get_by_id("Page", $id);
return $dataO->RelativeLink();
which also works.

Avatar
ipv89

Community Member, 6 Posts

24 November 2016 at 8:14am

Edited: 24/11/2016 8:16am

Still having issues with this :/ Thanks for the help so far.

If I var dump $dataO I get the object but if I vardump $dataO->AbsoluteLink() I receive a Server Error Page.I receive the same error with ->RelativeLink()

This is the vardump from $dataO

 {  
   [  
      "dataClass":protected
   ]   => string(4) "Page"   [  
      "dataQuery":protected
   ]   => object(DataQuery)#422 (8)   {  
      [  
         "dataClass":protected
      ]      => string(4) "Page"      [  
         "query":protected
      ]      => object(SQLQuery)#93 (12)      {  
         [  
            "isDelete":protected
         ]         => bool(false)         [  
            "select":protected
         ]         => array(0)         {  

         }         [  
            "groupby":protected
         ]         => array(0)         {  

         }         [  
            "having":protected
         ]         => array(0)         {  

         }         [  
            "distinct":protected
         ]         => bool(true)         [  
            "orderby":protected
         ]         => array(1)         {  
            [  
               ""               Sort""
            ]            => string(3) "ASC"
         }         [  
            "limit":protected
         ]         => array(0)         {  

         }         [  
            "where":protected
         ]         => array(1)         {  
            [  
               0
            ]            => array(1)            {  
               [  
                  "`ID` = '15'"
               ]               => array(0)               {  

               }
            }
         }         [  
            "connective":protected
         ]         => string(3) "AND"         [  
            "from":protected
         ]         => array(1)         {  
            [  
               "SiteTree"
            ]            => string(10) ""SiteTree""
         }         [  
            "replacementsOld":protected
         ]         => array(0)         {  

         }         [  
            "replacementsNew":protected
         ]         => array(0)         {  

         }
      }      [  
         "collidingFields":protected
      ]      => array(0)      {  

      }      [  
         "queriedColumns":         "DataQuery":private
      ]      => NULL      [  
         "queryFinalised":         "DataQuery":private
      ]      => bool(false)      [  
         "querySubclasses":protected
      ]      => bool(true)      [  
         "filterByClassName":protected
      ]      => bool(true)      [  
         "queryParams":         "DataQuery":private
      ]      => array(2)      {  
         [  
            "Versioned.mode"
         ]         => string(5) "stage"         [  
            "Versioned.stage"
         ]         => string(4) "Live"
      }
   }   [  
      "model":protected
   ]   => object(DataModel)#3 (1)   {  
      [  
         "customDataLists":protected
      ]      => array(0)      {  

      }
   }   [  
      "inAlterDataQueryCall":protected
   ]   => bool(false)   [  
      "failover":protected
   ]   => NULL   [  
      "customisedObject":protected
   ]   => NULL   [  
      "objCache":      "ViewableData":private
   ]   => array(0)   {  

   }   [  
      "class"
   ]   => string(8) "DataList"   [  
      "extension_instances":protected
   ]   => array(0)   {  

   }   [  
      "beforeExtendCallbacks":protected
   ]   => array(0)   {  

   }   [  
      "afterExtendCallbacks":protected
   ]   => array(0)   {  

   }
}object(DataList)#427 (11){  
   [  
      "dataClass":protected
   ]   => string(4) "Page"   [  
      "dataQuery":protected
   ]   => object(DataQuery)#424 (8)   {  
      [  
         "dataClass":protected
      ]      => string(4) "Page"      [  
         "query":protected
      ]      => object(SQLQuery)#432 (12)      {  
         [  
            "isDelete":protected
         ]         => bool(false)         [  
            "select":protected
         ]         => array(0)         {  

         }         [  
            "groupby":protected
         ]         => array(0)         {  

         }         [  
            "having":protected
         ]         => array(0)         {  

         }         [  
            "distinct":protected
         ]         => bool(true)         [  
            "orderby":protected
         ]         => array(1)         {  
            [  
               ""               Sort""
            ]            => string(3) "ASC"
         }         [  
            "limit":protected
         ]         => array(0)         {  

         }         [  
            "where":protected
         ]         => array(1)         {  
            [  
               0
            ]            => array(1)            {  
               [  
                  "`ID` = '15'"
               ]               => array(0)               {  

               }
            }
         }         [  
            "connective":protected
         ]         => string(3) "AND"         [  
            "from":protected
         ]         => array(1)         {  
            [  
               "SiteTree"
            ]            => string(10) ""SiteTree""
         }         [  
            "replacementsOld":protected
         ]         => array(0)         {  

         }         [  
            "replacementsNew":protected
         ]         => array(0)         {  

         }
      }      [  
         "collidingFields":protected
      ]      => array(0)      {  

      }      [  
         "queriedColumns":         "DataQuery":private
      ]      => NULL      [  
         "queryFinalised":         "DataQuery":private
      ]      => bool(false)      [  
         "querySubclasses":protected
      ]      => bool(true)      [  
         "filterByClassName":protected
      ]      => bool(true)      [  
         "queryParams":         "DataQuery":private
      ]      => array(2)      {  
         [  
            "Versioned.mode"
         ]         => string(5) "stage"         [  
            "Versioned.stage"
         ]         => string(4) "Live"
      }
   }   [  
      "model":protected
   ]   => object(DataModel)#3 (1)   {  
      [  
         "customDataLists":protected
      ]      => array(0)      {  

      }
   }   [  
      "inAlterDataQueryCall":protected
   ]   => bool(false)   [  
      "failover":protected
   ]   => NULL   [  
      "customisedObject":protected
   ]   => NULL   [  
      "objCache":      "ViewableData":private
   ]   => array(0)   {  

   }   [  
      "class"
   ]   => string(8) "DataList"   [  
      "extension_instances":protected
   ]   => array(0)   {  

   }   [  
      "beforeExtendCallbacks":protected
   ]   => array(0)   {  

   }   [  
      "afterExtendCallbacks":protected
   ]   => array(0)   {  

   }
} 

Avatar
blackduck

Community Member, 13 Posts

24 November 2016 at 12:20pm

How about posting the code that $dataO is part of, show some class :).

Go to Top