Jump to:

22984 Posts in 11765 Topics by 2827 members

General Questions

SilverStripe Forums » General Questions » $DataObject::get() Question

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

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

Page: 1 2
Go to End
Author Topic: 4373 Views
  • SSadmin
    Avatar
    Community Member
    90 Posts

    $DataObject::get() Question Link to this post

    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

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: $DataObject::get() Question Link to this post

    $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'");
    ...

  • SSadmin
    Avatar
    Community Member
    90 Posts

    Re: $DataObject::get() Question Link to this post

    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..

  • Pigeon
    Avatar
    Community Member
    243 Posts

    Re: $DataObject::get() Question Link to this post

    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.

  • SSadmin
    Avatar
    Community Member
    90 Posts

    Re: $DataObject::get() Question Link to this post

    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..

  • bunheng
    Avatar
    Community Member
    69 Posts

    Re: $DataObject::get() Question Link to this post

    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

  • secuaz
    Avatar
    Community Member
    21 Posts

    Re: $DataObject::get() Question Link to this post

    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!

  • Willr
    Avatar
    Forum Moderator
    5462 Posts

    Re: $DataObject::get() Question Link to this post

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

    4373 Views
Page: 1 2
Go to Top

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

Comments on this website? Please give feedback.