Login | Forgot password | Register

X

What is OpenID?

OpenID is an Internet-wide identity system that allows you to sign in to many websites with a single account.

With OpenID, your ID becomes a URL (e.g. http://username.myopenid.com/). You can get a free OpenID for example from myopenid.com.

For more information visit the official OpenID site.

Jump to:

1155 Posts in 324 Topics by 274 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Error with Count() and member functions

Page: 1
Go to End
Author Topic: Error with Count() and member functions 427 Views
  • fishe
    avatar
    Community Member
    42 posts

    Error with Count() and member functions Link to this post

    I have the following function in a page controller:

       function newCount() {
          $outfits = DataObject::get('OutfitPage');
          $outfitCount = $outfits->Count() - 1;
          return $outfitCount ? $outfitCount : 0;
       }

    and it's giving the following error:

    Fatal error: Call to a member function Count() on a non-object in /home/robynmat/public_html/ss/mysite/code/OutfitHolder.php on line 41

    Any idea of the possible reasons for this? As far as I can see it was working fine that code until a larger amount of objects (OutfitPage) were created (i.e. around 30 or so). Or maybe something else changed but I can't think of what.

    Help/ideas would be much appreciated

    Cheers

  • Sean
    avatar
    Core Development Team
    585 posts

    Re: Error with Count() and member functions Link to this post

    You should check for the existance of the object before calling Count() on it. It's also possible to optimise your code further as well.

    Here's an example:

    function newCount() {
       $outfits = DataObject::get('OutfitPage');
       return ($outfits) ? ($outfits->Count() - 1) : 0;
    }

    427 Views
Page: 1
Go to Top

Currently Online: motly, jondbaker, tf22raptor

Welcome to our latest member: fearofbuttons

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.