Jump to content

Deleting all rows in a repeater via API


a-ok
 Share

Recommended Posts

Does anyone know why the following doesn't work? The page would be called via a cronjob...

if ($input->get->run) {

	// Reset submissions
	if ($input->get->run == "resetSubmissions") {

		$submissions = $pages->get("template=home")->submissions;
		$submissions->of(false);
		$submissions->removeAll();
		$submissions->save();

		bdb('resetSubmissions has been run');

	}

}

 

Link to comment
Share on other sites

3 minutes ago, dragan said:

Well, does it work if you run it normally (not as cron job)? Do you get errors? What are the permissions of the page this runs in?

No, sorry. I haven't set up the cronjob yet... just testing the code. It returns:

Exception: Method RepeaterPageArray::of does not exist or is not callable in this context

 

Link to comment
Share on other sites

15 minutes ago, dragan said:

Well, then just remove that line. If you're deleting, it makes no sense anyway.

Sorry, I'm confused.

I'd like to remove all rows but keep the repeater. Upon removing the ->of(false) it still doesn't delete all the rows.

Link to comment
Share on other sites

I wonder if you should rather, delete() every page also, not use removeAll() because I think it's just removing it from the object, not from the db.

foreach($pages->get("template=home")->submissions as $s){
	$s->delete();
}

 

  • Thanks 1
Link to comment
Share on other sites

3 minutes ago, elabx said:

I wonder if you should rather, delete() every page also, not use removeAll() because I think it's just removing it from the object, not from the db.


foreach($pages->get("template=home")->submissions as $s){
	$s->delete();
}

 

Thanks. This doesn't seem to work either, weirdly.

$submissions = $pages->get("template=home")->submissions;
foreach($submissions as $s) {
	$s->delete();
}
$submissions->save();

 

Link to comment
Share on other sites

$submissions = $pages->get("template=home")->submissions;
foreach ($submissions as $s) {
	$s->delete();
	$s->save();
}
$pages->get("template=home")->save();

This worked! But so many saves? I never know.

Link to comment
Share on other sites

5 hours ago, a-ok said:

$submissions->of(false);

of() is a method of Page objects, but you're trying to use it on an PageArray of Repeater pages. Try:

// Get the page you want to work with
$home = $pages->get("template=home");
// Turn output formatting off for the page
$home->of(false);
// Change the Repeater field value
$home->submissions->removeAll();
// Save the page
$home->save();

 

  • Thanks 1
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

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...