Jump to content

Delete entire contents of array with API


onjegolders
 Share

Recommended Posts

Hi, is there any way of deleting the entire contents of an array from the API?

I have had some success removing an item with:

$page->image->remove($page->image->first());

But as I have to specify the item within remove() I'm not sure how to remove all the items.

Thanks

Link to comment
Share on other sites

Sorry guys,

The background is that I've set up add student/edit student/delete student front-end forms for my editor.

One of the fields is a multiselect where they choose subjects (which are pages).

If the student already has Maths/English

and on the edit profile form, my editor just chooses English, I'd like it so that that student only has English on their profile.

Essentially if the editor chooses to change the subjects, I need to delete the existing ones (there may be 1, 2, 10 etc) before adding only the one(s) that the editor has chosen, otherwise the form simply adds the subject to the existing ones.

God it's tough to explain code sometimes in words!

EDIT: I actually tried "unset" as well as setting an empty array but couldn't get either to work

Link to comment
Share on other sites

  • 9 months later...
$page->images->removeAll();
$page->save();
 

I used this on a single image field, and it didn't work at all. My guess is that it happens because in that case the field returns a single image and not an array, and removeAll() fails.

After lots of head scratching (second time in this thread), and searching on the forums, I found this thread and managed to make it work based on my own advice fours posts above. How ironic is that, hein?

$page->of(false);

$page->image = $page->image->makeNew(); // <- added this
$page->save();

$u = new WireUpload('pic');
$u->setMaxFiles(1);
$u->setOverwrite(false);
$u->setDestinationPath($page->image->path());
$u->setValidExtensions(array('jpg', 'jpeg', 'gif', 'png'));
foreach($u->execute() as $filename) $page->image = $filename;

$page->save();
 
  • Like 1
Link to comment
Share on other sites

*Note for advanced users: the single image behavior described above is only applicable when output formatting is ON, as it is by default in template files. When output formatting is OFF, image fields always behave as arrays. 

Thanks Soma for reminding us. This is right here in the docs but I always forget! :)

Link to comment
Share on other sites

  • 1 year later...

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