Jump to:

3372 Posts in 998 Topics by 712 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Help With Where Clause In DataObject::get

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

Page: 1
Go to End
Author Topic: 6253 Views
  • Liam
    Avatar
    Community Member
    470 Posts

    Help With Where Clause In DataObject::get Link to this post

    The code:

    DataObject::get('Page', "`ClassName` IN ('VideoPage', 'ArticlePage', 'EventPage', 'MusicPage') AND `FeaturedHomePage` = 1", 'Created DESC', null, $number);

    Getting the error that the where clause is ambiguous. I understand that it is caused because each pagetype has the same field "FeaturedHomePage".

    I'm trying to return only the 4 pagetypes wherever FeaturedHomePage is true.

    How do I rewrite this?

  • Phalkunz
    Avatar
    69 Posts

    Re: Help With Where Clause In DataObject::get Link to this post

    Why don't you have "FeaturedHomePage" field on Page model instead of having this field on its subclasses?

  • Liam
    Avatar
    Community Member
    470 Posts

    Re: Help With Where Clause In DataObject::get Link to this post

    The reason was that only those 4 pagetypes have the FeaturedHomePage field. I have more than 4 custom page types, so was trying to keep it clean and organized.

    Is it better practice to add it to the page type and let every custom page type inherit it even when it's not needed?

    Is there no other way to pull the data I want from the 4 custom page types with the FeaturedHomePage field set to true?

  • Phalkunz
    Avatar
    69 Posts

    Re: Help With Where Clause In DataObject::get Link to this post

    I see. Well, for me I would create new subclass of page type with FeaturedHomePage field and let these 'VideoPage', 'ArticlePage', 'EventPage', 'MusicPage' page types subclass the new page type.

    Or you can pull in one page type at a time and combine them.

    $featuredVideoPages= DataObject::get('VideoPage', "`FeaturedHomePage` = 1", 'Created DESC', null, $number);
    $featuredArticlesPage= DataObject::get('ArticlePage', "`FeaturedHomePage` = 1", 'Created DESC', null, $number);
    $featuredEventPages= DataObject::get('EventPage', "`FeaturedHomePage` = 1", 'Created DESC', null, $number);
    $featuredMusicPages= DataObject::get('MusicPage', "`FeaturedHomePage` = 1", 'Created DESC', null, $number);

    // combine them
    ...

    Another way is you can use raw sql with UNION keyword.

  • Liam
    Avatar
    Community Member
    470 Posts

    Re: Help With Where Clause In DataObject::get Link to this post

    Thanks for the suggestions. I think I'll go with your former suggestion in your 2nd post. I'd prefer to stay away from raw sql and use the built in stuff and obviously the less queries the better.

    Just wasn't sure if there was a way to rewrite it with one query like how I was going initially.

    Is there a way to block page types from being created in the admin section? If I setup this new subclass of the page model for these 4 pages to inherit, it's going to show up in the admin which I don't really want since it won't be used per se. Would prefer to keep the options simple for the content editors using the admin.

    Think this has been brought up before, but a quick search can't find anything.

  • Phalkunz
    Avatar
    69 Posts

    Re: Help With Where Clause In DataObject::get Link to this post

    You might wanna open a ticket for this option (hide a page type in cms).

    6253 Views
Page: 1
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.