Jump to:

3372 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Get a certain page matching some criteria

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

Page: 1
Go to End
Author Topic: 1204 Views
  • Boogiez
    Avatar
    Community Member
    17 Posts

    Get a certain page matching some criteria Link to this post

    Hey all,

    So I have a section on the home page of the site I'm building which is supposed to allow the admin to enter a reference number in a box on the CMS, such that when the page is rendered it will find a page of type Product or MultipleSizeProduct with that reference number and return that page so I can display the associated image for a featured design page.

    I'm most of the way there, but having a little trouble. I wrote a function to return that child:

       function FirstItem() {
          return = DataObject::get("Product", "ReferenceNumber = " . $this->FeaturedItemOne);
       }

    With template code:

    <% control FirstItem %>
    // do stuff in here
    <% end_control %>

    This works fine, but I also want it to be able to find a MultipleSizeProduct with $ReferenceNumber equal to $this->FeaturedItemOne, and be able to fail gracefully if the administrator puts in a reference number that doesn't match any product (displaying nothing would be fine)... I just don't want the "Error: the webserver has not been able to respond to your request" error. I tried to do something like this:

       function FirstItem() {
          $do = DataObject::get("Product", "ReferenceNumber = " . $this->FeaturedItemOne);
          if ($do.isEmpty()) $do = DataObject::get("MultiSizeProduct", "ReferenceNumber = " . $this->FeaturedItemOne);
          if (!$do.isEmpty()) return $do;
          else return false;
       }

    But isEmpty() is not recognized for some reason. This seems like it should be a pretty easy answer for someone who's well versed in the data model. Anyone?

  • Hamish
    Avatar
    Community Member
    712 Posts

    Re: Get a certain page matching some criteria Link to this post

    Been programming in VB? You're using the dot operator in a 'VB' kind of way - in PHP the dot appends strings. To call methods on objects you use "->". Also, I'm pretty sure that if no objects are found, DataObject::get returns false, so this should work:

    function FirstItem() {
       $do = DataObject::get("Product", "ReferenceNumber = " . $this->FeaturedItemOne);
       if (!$do) $do = DataObject::get("MultiSizeProduct", "ReferenceNumber = " . $this->FeaturedItemOne);
       return $do;
    }

    Note that you don't need to return false, since $do will already be false if the object doesn't exist.

    Could even compress to:

    function FirstItem() {
       $do = DataObject::get("Product", "ReferenceNumber = " . $this->FeaturedItemOne);
       return $do ? $do : DataObject::get("MultiSizeProduct", "ReferenceNumber = $this->FeaturedItemOne");
    }

  • Boogiez
    Avatar
    Community Member
    17 Posts

    Re: Get a certain page matching some criteria Link to this post

    Thanks for your response!

    Unfortunately, I have the same problem as before. It works fine for selecting a page of type Product, but if I set FeaturedItemOne in the CMS to the ReferenceNumber of a MultipleSizeProduct, I get "Error. The webserver has not been able to respond to your request." on the page calling the functions.

    I didn't mention this in my original post, but I think the most desirable behavior if a bad FeaturedItemOne value is given (say, 0, or a ReferenceNumber that does not correspond to any Product or MultipleSizeProduct) would be to simply not have any child given. So for example if both FeaturedItemOne and FeaturedItemTwo were bad values, it would be like my template simply did not have have <% control Children %> statements in it. The logic is certainly not difficult but I'm not quite sure what should be returned to facilitate this.

    Thanks again!

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: Get a certain page matching some criteria Link to this post

    Put the site into Development mode (add Director::set_environment_type("dev"); to your mysite/_config.php file and you should get a proper error message. Could just be a typo

  • Boogiez
    Avatar
    Community Member
    17 Posts

    Re: Get a certain page matching some criteria Link to this post

    Thanks, Will. I feel kind of embarrassed because it was such a stupid error. My page name is "MultipleSizeProduct" not "MultiSizeProduct." Thanks for your help with writing the function, Hamish!

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