Unable to delete pages programmatically (v5.5.1)

Permalink
When I try using the Page::delete() function, I get this error message:

Fatal error: Call to a member function getAttributeKeyID() on a non-object in [root]/concrete/models/collection.php on line 198

I've looked through the collection and page models, I can't figure out why this would be happening. Anyone run into this before or know how to solve it?

By the way this is my related code, running from a simple block I set up for this purpose:
$c = Page::getCurrentPage();
while($delPage = $c->getFirstChild()) {
   $delPage->delete();
}


If there's a better way to delete all pages beneath a certain page, I'll take that as well.

ZeusExMachina
View Replies:
speedytwenty replied on at Permalink Reply
I am having this same issue as well.
dzimney replied on at Permalink Reply
dzimney
Not exactly sure why you'd be getting that error, but looking at your code, your while loop would never end because the action of setting a variable, even to null, will always equate to TRUE.

You'd want to do something more like this,

$c  = Page::getCurrentPage();
$child = $c->getFirstChild();
while( $child != null ) {
   $child->delete();
   $child = $c->getFirstChild();
}
ZeusExMachina replied on at Permalink Reply
ZeusExMachina
Actually, the basic assignment operator returns whatever the variable was set to. Note inhttp://www.php.net/manual/en/language.operators.assignment.php... that the expression inside the parentheses on the first example evaluates to 4.

A common use of the type of loop I used is retrieving rows from a database query, such as
while($row = $mysqli_result->fetch_assoc()) {
 //...
}

When there are no more rows, fetch_assoc() returns null, $row is set to null, and the expression evaluates to null, exiting the loop. See first example athttp://php.net/manual/en/mysqli-result.fetch-assoc.php...
dzimney replied on at Permalink Reply
dzimney
Sorry, I was confusing my languages there.