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.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

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

$DataObject::get() Question


Go to End


16 Posts   21299 Views

Avatar
SSadmin

Community Member, 90 Posts

29 January 2010 at 3:34pm

Hey,geeks. I met a problem try to select the useful information i want from the database.
I am using $DataObject::get("ListingPage",$filter)

Whats the right format to provide $filter here?! i wannto use the where statement: "where ListingPage.ListingType=Business"..
But not sure how to put them in the function()..

any suggestions?!
Thanks

Avatar
Willr

Forum Moderator, 5523 Posts

29 January 2010 at 4:36pm

Edited: 29/01/2010 4:37pm

$filter is basically just added into the SQL WHERE statement. So the format is standard sql without the 'WHERE'. For example

DataObject::get('ListingPage', "ListingType = 'Business'");

You can combine more then 1 command in the filter... eg

DataObject::get('ListingPage',"ListingType = 'Something' AND AnotherColumn = '2'");
...

Avatar
SSadmin

Community Member, 90 Posts

9 February 2010 at 11:07am

I encountered a problem with Dataobject again.
I want to grab a sortted dataset (byListingDate DESC) from database frist.
can do it by using:
$listings=DataObject::get("ListingPage","","ListingDate DESC");

Then select the ListingPage items by different Categories.
So i did

$filter="ListingType='Business'";

$records= DataObject::get($listings,$filter);

return $records;

i assume it should give me the result i want, because the $listings should also be a "ListingPage" Type object and as well as the "$records". But seems the result is a little bit strange..

Donot know whats the good way to handle the twice DB queries. anysuggestions will be appreciated..

Avatar
dhensby

Community Member, 253 Posts

9 February 2010 at 12:15pm

Edited: 11/02/2010 3:30pm

Hi, you seem to have the wrong end of the stick with the DataObject::get() method.

Have a look at this: http://doc.silverstripe.org/doku.php?id=datamodel#querying_data

Simply, you define you DataObject class, filter, sort order, join and limit all in one function (but seperate parameters, of course). All the parameters take strings and all are optional except the first parameter.

So what you want to do, seems to be:

DataObject::get('ListingPage','ListingType = "Business"','ListingDate DESC');

Thus getting all the listing pages where column 'ListingType' is 'Business' and then sorted by ListingDate with newest at the top.

Hope that clears that up.

Avatar
SSadmin

Community Member, 90 Posts

9 February 2010 at 2:36pm


Hey.thx, Pigeon.

I was totally wrong about the things i am archiving.

Originally, i think it could be helpful, we can sort of the listing pages by date desc then the lastest come up first. and we can sort of the lastest items by different categories.. But acutally its stupid..

Its better to do DataObject::get("ListingPage",$fitler,"ListingDate DESC");

Thanks for ur suggestion and answer for my stupid logical problem..

Avatar
bunheng

Community Member, 78 Posts

4 February 2011 at 10:35pm

Good morning from Cambodia

I did the following code for to get pages display on my home page

class HomePage_Controller extends Page_Controller {
function GetPageList(){
$PageList = DataObject::get("Page", "ShowOnFrontPage=1", "LastEdited DESC", "", 4);
return $PageList;
}
}

inside HomePage.ss i use

<% control GetPageList %>
$Title
<% end_control %>

But i see only one results.

Could you please show the trick.

Bunheng

Avatar
secuaz

Community Member, 24 Posts

2 May 2013 at 1:50am

Hi all!!

I am trying to make this query in silverstripe but it doesn't want to work:

$entries = BookstoreBook::get()
->sort($order)
->where('"BookstoreBook"."Title" LIKE $searchString');
return $entries ? $entries : false;

How can I use "LIKE" in the where function?????
Thank you Please help!

Avatar
Willr

Forum Moderator, 5523 Posts

3 May 2013 at 9:00pm

$entries = BookstoreBook::get()
->where("Title LIKE '$searchString'")
->sort($order);

Go to Top