Пример #1
0
 /**
  * Sets main image of model
  * @param $img
  * @throws \Exception
  */
 public function setMainImage($img)
 {
     if ($this->owner->{$this->idAttribute} != $img->item_id) {
         throw new \Exception('Image must belong to this model');
     }
     $counter = 1;
     /* @var $img Image */
     $img->setMain(true);
     $img->url_alias = $this->getAliasString() . '-' . $counter;
     $img->save();
     // set all other images related to this model and id to NOT be main
     Image::updateAll(['is_main' => 0], ['and', ['model_name' => $img->model_name, 'item_id' => $img->item_id], ['not', ['url_alias' => $img->url_alias]]]);
     $this->owner->clearImagesCache();
 }