gebeer Posted October 10, 2014 Share Posted October 10, 2014 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 More sharing options...
marcus Posted October 10, 2014 Share Posted October 10, 2014 Possibly $severad has sub-pages. If so, try: ...if ($serverad->id) $pages->delete($serverad, true);... See: http://cheatsheet.processwire.com/pages/built-in-methods-reference/pages-delete-page-true/ Link to comment Share on other sites More sharing options...
gebeer Posted October 10, 2014 Author Share Posted October 10, 2014 Thanks for that hint. But no subpages. I also tried $pages->delete($serverad). Same result. EDIT: and $pages->delete($serverad, true) Link to comment Share on other sites More sharing options...
Soma Posted October 10, 2014 Share Posted October 10, 2014 You delete the first so second reference in find does NOT work cause its not there anymore... Link to comment Share on other sites More sharing options...
gebeer Posted October 10, 2014 Author Share Posted October 10, 2014 @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 More sharing options...
Soma Posted October 10, 2014 Share Posted October 10, 2014 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. Link to comment Share on other sites More sharing options...
Soma Posted October 10, 2014 Share Posted October 10, 2014 First get the the pages you want to delete and then delete them.. not searching a reference with a page you already deleted. 1 Link to comment Share on other sites More sharing options...
gebeer Posted October 10, 2014 Author Share Posted October 10, 2014 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 Link to comment Share on other sites More sharing options...
LostKobrakai Posted October 10, 2014 Share Posted October 10, 2014 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 More sharing options...
Soma Posted October 10, 2014 Share Posted October 10, 2014 I have lots troubles writing on my new mobile. It's horrible. Link to comment Share on other sites More sharing options...
gebeer Posted October 10, 2014 Author Share Posted October 10, 2014 no swipe keyboard? Mine is also auto correcting and garbling things 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