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.

Data Model Questions /

Get a certain page matching some criteria


Reply


5 Posts   1305 Views

Avatar
Boogiez

Community Member, 17 Posts

10 February 2009 at 8:10pm

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? :)

Avatar
Hamish

Community Member, 712 Posts

14 February 2009 at 1:55pm

Edited: 14/02/2009 2:03pm

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

Avatar
Boogiez

Community Member, 17 Posts

14 February 2009 at 4:35pm

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! :)

Avatar
Willr

Forum Moderator, 5513 Posts

15 February 2009 at 7:23pm

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

Avatar
Boogiez

Community Member, 17 Posts

16 February 2009 at 5:24pm

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!