infoweb\cms\behaviors\ImageBehave::setMainImage PHP Method

setMainImage() public method

Sets main image of model
public setMainImage ( $img )
$img
    public function setMainImage($img)
    {
        if ($this->owner->primaryKey != $img->itemId) {
            throw new \Exception('Image must belong to this model');
        }
        $where = ['itemId' => $this->owner->id, 'modelName' => StringHelper::basename($this->owner->className())];
        // Check if the main image is already set
        $mainImage = $img->findOne(yii\helpers\ArrayHelper::merge(['isMain' => 1], $where));
        if ($mainImage) {
            return false;
        }
        // Reset main image
        $img->updateAll(['isMain' => 0], $where);
        // Clear images cache
        $this->owner->clearImagesCache();
        // Set new main image
        $img->setMain(true);
        return $img->save();
    }