Jump to content

Recommended Posts

Posted

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.

Posted

Thanks for that hint.

But no subpages. I also tried $pages->delete($serverad). Same result.

EDIT:

and $pages->delete($serverad, true)

Posted

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

Posted

But second find won't return result.. ? Doesn't matter if page object is still in memory. Can't see anything else cause it's no problem to delete pages.

Posted

First get the the pages you want to delete and then delete them.. not searching a reference with a page you already deleted.

  • Like 1
Posted

Thank you Soma, now I got you.

I moved

$serverads = $pages->find("template=advertisement, ad_server={$serverId}");

before

$serverpage->delete()

And now everything is working :)

Posted

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.

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
×
×
  • Create New...