/** * Set attachment data for this block by array. * * @param array $attachmentsData Array of key-value arrays of data for each * attachment. * @param boolean $deleteExtras Whether to delete extra preexisting * attachments after setting new data. */ public function setAttachments($attachmentsData, $deleteExtras = true) { // We have to have an ID to proceed. if (!$this->exists()) { $this->save(); } $existingAttachments = $this->getAttachments(); foreach ($attachmentsData as $i => $attachmentData) { if (!empty($existingAttachments)) { $attachment = array_pop($existingAttachments); } else { $attachment = new ExhibitBlockAttachment(); $attachment->block_id = $this->id; } $attachment->setData($attachmentData); $attachment->save(); } if ($deleteExtras) { foreach ($existingAttachments as $extraAttachment) { $extraAttachment->delete(); } } }