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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

Random Items from DOM (Only from published pages)


Go to End


2 Posts   1838 Views

Avatar
drye

Community Member, 49 Posts

21 August 2009 at 3:16pm

I'm trying to get some random products from a DOM, however I want them to only be from currently published pages.

RIght now I have this:

	public function getSpecialItems($limit)
	{
	   //DataObject::get($obj, $filter, $sort, $join, $limit);
	   return DataObject::get("SpecialItem","SpecialHolderID != 5","RAND()","",$limit);
	}

Because my SpecialHolder with ID 5 isn't published and I don't want the results to include it. I'd rather this be more automatic and not hard coded. Any thoughts?

Avatar
UncleCheese

Forum Moderator, 4102 Posts

22 August 2009 at 3:27am

You need to use a join.

return DataObject::get(
"SpecialItem",
null,
"RAND()",
"INNER JOIN `SpecialHolder` ON `SpecialHolder`ID = SpecialItem.SpecialHolderID",
$limit);

It should automatically add the "_Live" suffix to SpecialHolder table, which will nullify the SpecialItem records that aren't attached to a _Live table.

Probably not the best way to do that, though.