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

Splitting DataObject using ModelAdmin


Go to End


4 Posts   896 Views

Avatar
swaiba

Forum Moderator, 1899 Posts

17 August 2011 at 11:51pm

Edited: 18/08/2011 4:28am

Hello everyone,

I have a DataObject (named Extra) that has very much outgrown it's original purpuse and has tons of entries (where it was supposed to have less than a hundred, but now is many thousands).

There is a enum type field and effective dates (and a couple of other "filters").

Currently it is one entry in the managed_models in ModelAdmin, but I keep getting asked to split it up.

It would be easier to modify the presentation of this within ModelAdmin rather than the code that is filled with DataObject::get_by_id('Extra'), $somedataobject->ExtraID and many other things.

So my question is how can I add two entries into the managed_models for Extra based on the type of Extra? or is there another way someone can think of to split this dataobject up for easier access/editting?

All help appreciated!

Avatar
swaiba

Forum Moderator, 1899 Posts

20 August 2011 at 2:52am

Answer to my own question...

Create several ModelAdmin's - set and override the collection controller and add this function...

function getSearchQuery($searchCriteria){
	$query = parent::getSearchQuery($searchCriteria);
	$query->where[] = "Type='Cash Voucher'";  //<-- changing for each type of ModelAdmin
	return $query;
}

Then to handle the increased number of menu items...

https://github.com/ajshort/silverstripe-groupedcmsmenu

Avatar
martimiz

Forum Moderator, 1391 Posts

20 August 2011 at 6:40am

Nice! Couldn't help thinking about it, and all the extra menuitems seemed like an obstacle to me. But then I never knew about the ajshort dropdown menu :-) To complete things: how do you handle new items getting the correct value for each type? A hidden field I guess?

Avatar
swaiba

Forum Moderator, 1899 Posts

23 August 2011 at 8:45am

how do you handle new items getting the correct value for each type? A hidden field I guess?

That part is fine - there are only 3/4 "Types" in the enum that need to be split out. So creating an individual ModelAdmin with a hardcoded extra where clause added in getSearchQuery is fine.