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

Quotation on every page using GridField


Reply

3 Posts   417 Views

Avatar
bones

7 March 2014 at 10:24am (Last edited: 7 March 2014 10:24am), Community Member, 104 Posts

Hello
I'm finally moving up to SS3 from 2.4! My previous site used DOM for a list of quotations, and I then used the following code in page.php to display a random quotation on every page.

public function RandomQuotes()
{
$limit = 1;
return DataObject::get("Quote",null,"RAND()",null,$limit);
}


I've got GridField working. But I can't work out how to modify the above code to give me the same result as previously.

All help appreciated. Thanks :)

Avatar
thomas.paulson

7 March 2014 at 5:40pm Community Member, 79 Posts

public function RandomQuotes()
{
$limit = 1;
return Quote::get()->sort('RAND()') ->limit($num);
}

Avatar
bones

8 March 2014 at 1:35am Community Member, 104 Posts

Thanks, Thomas. I've very grateful for your reply.

The code you supplied didn't work properly but you pointed me in the right direction. Here's the working version:

public function RandomQuotes()
{
return Quote::get()->sort('RAND()') ->limit(1);
}


Thanks again :D