Jump to content

Page does not get deleted


gebeer
 Share

Recommended Posts

Hi,

I want to delete 2 pages with $page->delete() after one another . But only one of the pages gets deleted.

My code

            $serverpage = $pages->get($serverId);
            if ($action == "delete") {
                if ($serverpage->id) {
                    $serverpage->delete(); //When I comment this out, $serverad further down gets deleted

                    //if ads for that server exist, delete them
                    $serverads = $pages->find("template=advertisement, ad_server={$serverId}");
                    
                    if (count($serverads) != 0) {
                        foreach ($serverads as $serverad) {
                            if ($serverad->id) $serverad->delete(); //does not get deleted when $serverpage->delete() above executes.
                        }
                        $serverout .= "<div class='alert alert-success' role='alert'>All Ads for the server deleted.</div>";
                    }

                    $serverout .= "<div class='alert alert-success' role='alert'>Server successfully deleted.</div>";
                } else {
                    $serverout .= "<div class='alert alert-danger' role='alert'>Server does not exist and could not be deleted.</div>";
                }

                $serverout .= $serverForm->render();
            }

When $serverpage->delete() is executed, $serverad->delete() has no effect.

When I comment out $serverpage->delete(), $serverad->delete() is working.

I don't get any errors and I already checked that $serverad is a valid page object in all cases. Now I'm clueless of what the problem might be.

Link to comment
Share on other sites

@Soma

I already checked that page object for second page is still there after I delete first page. They are independent from another and $serverID is defined before the other stuff and is not related to the first page that gets deleted.

$serverpage and $serverad live under different parents in the tree and both never have children.

Link to comment
Share on other sites

I had to think about Somas answers a bit, too. To clarify for others looking at this: Deleting the page also removes the page from all pagefields it's assigned to. That's why the find doesn't return the expected results, even though the id is still saved as a variable.

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...