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 /

Help With Where Clause In DataObject::get


Reply


6 Posts   6493 Views

Avatar
Liam

Community Member, 470 Posts

6 June 2009 at 4:41pm

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?

Avatar
Phalkunz

69 Posts

8 June 2009 at 9:49am

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

Avatar
Liam

Community Member, 470 Posts

8 June 2009 at 11:09am

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?

Avatar
Phalkunz

69 Posts

8 June 2009 at 11:35am

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.

Avatar
Liam

Community Member, 470 Posts

8 June 2009 at 1:02pm

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.

Avatar
Phalkunz

69 Posts

8 June 2009 at 1:42pm

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