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

Error with Count() and member functions


Go to End


2 Posts   3059 Views

Avatar
fishe

Community Member, 42 Posts

18 March 2009 at 8:52pm

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

Avatar
Sean

Forum Moderator, 922 Posts

23 March 2009 at 9:20pm

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