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.

Data Model Questions /

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

Adding a default where clause to dataobject->get


Go to End


2 Posts   1352 Views

Avatar
jaybee

Community Member, 49 Posts

23 July 2016 at 4:35pm

I'd like to add a default where clause to all dataobjects so I don't have to write things like this everytime: DataObject::get()->where('Archived = 0')
How would you go about it? I was thinking of creating a BaseDataObject that extends DataObject and overrides the get method.
Anyone done anything similar before?

Thanks for any help/advice

Avatar
jaybee

Community Member, 49 Posts

6 August 2016 at 5:50pm

Just ended up creating a wrapping function that's available in one of my base files as I needed paginated results as well.

public function MyList($request, $filters=[], $sort=[], $where="Deleted IS NULL", $limit=20) {
	$list = DataObject::get()->where($where)->filter($filters)->sort($sort);
		
        $results = PaginatedList::create(
            $list,
            $request
        )->setPageLength($limit);

        return $results;
}