/** * Update gallery images. * @return void */ private function updateImages() { $old = []; foreach ($this->object->images as $image) { $old[$image->id] = $image; } // insert/update foreach ($this->images as $data) { $id = null; if (!empty($data['id'])) { $id = $data['id']; } if (array_key_exists($id, $old)) { $image = $old[$id]; unset($old[$id]); } else { $image = new GalleryImage(); $image->gallery_id = $this->object->id; } $image->setAttributes($data); Yii::$app->storage->storeObject($image); $image->save(false); } // delete foreach ($old as $image) { Yii::$app->storage->removeObject($image); $image->delete(); } }
/** * Images relation * @return ActiveQuery */ public function getImages() { return $this->hasMany(GalleryImage::className(), ['gallery_id' => 'id']); }