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

Unsetting DataObjects when iterating through a DataObjectSet


Go to End


2 Posts   3384 Views

Avatar
MarcusDalgren

Community Member, 288 Posts

1 July 2009 at 9:04pm

Hello.

I have been following the tutorials and I am using one of the functions listed to get the latest news items on different pages.
However, I only want to show the news items that the user has access to. Figuring out if the current item could be viewed was really easy but getting rid of the offending news items proved to be harder.

I ended up creating a new DataObjectSet and putting the DataObjects that the user can view in the new set and then returning that, but I am thinking that there must be an easy way to unset a DataObject during a foreach loop but I just can't find it.

Also is there an easy way to restrict a DataObject::get() to only get the records that the user is allowed to view so I don't have to filter after the fact?

Kindly, Marcus.

Avatar
Willr

Forum Moderator, 5523 Posts

2 July 2009 at 12:06pm

DataObject::get() to only get the records that the user

Not well, I always just do a foreach loop after it if($object->can('View')) ...;

To unset it can't you just call remove? I don't think PHP will complain about concurrency errors with the following

foreach($pages as $page) {
  $pages->remove($page);
}