adrian Posted July 2, 2014 Share Posted July 2, 2014 Hi everyone, I know that: $this->pages->delete($parent_page, true); mostly works, but the problem is when one of the child pages is not deletable - you get an error and the script terminates. So I thought I'd write a custom approach to delete pages by order of number of path segments, so that children are always deleted before their parents, like this: // create empty page array $res = new PageArray(); foreach($pagestodelete as $item) { // temporarily add pathsegments property to items $item->pathsegments = count(explode('/',$item->path)); $res->add($item); } //sort based on their level in the page hierarchy so we can delete children before their parents $items = $res->filter("sort=-pathsegments"); foreach($items as $item){ if($item->deleteable()) { $item->delete(); } } This seems like it should work, but the deleteable() check is not excluding pages as it should, so I am getting a can't be deleted error. Also, even though all the children of a page have been deleted first, I still get the "Can't delete Page $page because it has one or more children" error. Maybe I am going about this all wrong, but my brain has stopped working now! Thanks very much to anyone with any ideas Link to comment Share on other sites More sharing options...
adrian Posted July 2, 2014 Author Share Posted July 2, 2014 I should remember to just always look at the PW source before asking a question Putting the following right after I delete does the trick. Still it seems clunky to have to do this - is there something I am missing? $item->status = Page::statusDeleted; I take that back - I don't think it works at all EDIT: Can anyone see why the delete method has to throw an exception when a child page is not deleteable. Why not just issue a warning and continue with the next child? 1 Link to comment Share on other sites More sharing options...
ryan Posted August 22, 2014 Share Posted August 22, 2014 EDIT: Can anyone see why the delete method has to throw an exception when a child page is not deleteable. Why not just issue a warning and continue with the next child? I might not be totally understanding the scenario, but if a child can't be deleted then the parent definitely can't be deleted either. Otherwise, you'd end up with orphaned pages in the DB. So there wouldn't be much purpose in issuing a warning or doing anything other than halting immediately. Something that would result in orphaned pages in the DB should be a fatal error, at least in my opinion. Link to comment Share on other sites More sharing options...
LostKobrakai Posted August 24, 2014 Share Posted August 24, 2014 I think adrian meant a more complex solution, where every deletable children would be removed and only the non-deletable ones and it's parents would stay, so that no pages get orphaned. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now