Jump to content

$pages->delete throws Exception although there is no child anymore


foxcraft_aw
 Share

Recommended Posts

Hello!

I have a strange problem with the "delete" method of the "pages" API-variable: When I insert a parent- and a child-page, and then immediately try to delete first the child- and then the parent-page, an exception is thrown telling me that the parent cannot be deleted, because it still contains a child-page.

You can reproduce this behaviour using the following code:

<?php

require 'index.php';

$parentPage = new ProcessWire\Page;
$parentPage->name = 'parent';
$parentPage->template = 'basic-page';
$parentPage->title = 'parent';
$parentPage->parent = $pages->get('/');
$parentPage->save();

$childPage = new ProcessWire\Page;
$childPage->name = 'child';
$childPage->template = 'basic-page';
$childPage->title = 'child';
$childPage->parent = $pages->get('/parent');
$childPage->save();

$pages->delete($pages->get('/parent/child'));
$pages->delete($pages->get('/parent'));

(I put this code into a file called "experiments.php" in the root directory of the site, because I do not know of any better way for quickly testing out code in ProcessWire yet)

 

Can you tell me why this code throws an exception?
What am I missing?

I am using ProcessWire 3.

Thanks for your help!

 

PS: Yes, I know about the second parameter in the "$pages->delete" method, but it should not be necessary to be set in this scenario if I`m correct.

Link to comment
Share on other sites

  On 7/11/2017 at 12:51 PM, BitPoet said:

Does uncaching the parent page fix it?

$pages->delete($pages->get('/parent/child'));

$prnt = $pages->get('/parent');
$pages->uncache($prnt);
$pages->delete($prnt);

 

Expand  

Thanks for the tip!
It does not work when I only uncache the parent, but it does work when I simply uncache all pages:

$pages->delete($pages->get('/parent/child'));

$pages->uncache();
$pages->delete($pages->get('/parent'));

 

I did not know there was some caching going on behind the scenes. Do you know about any documentation on this mechanism?

 

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...