Пример #1
0
 /**
  * @param $path
  * @param bool|false $primary
  * @return bool
  * @throws \yii\db\Exception
  */
 public function attachImage($path, $primary = false)
 {
     $image = new Image();
     if (!$image->uploadFrom($path)) {
         return false;
     }
     $itemId = $this->owner->primaryKey;
     $model = $this->getModelShortName();
     $query = (new Query())->from($this->junctionTable)->where(['model' => $model, 'item_id' => $itemId]);
     $sort = $primary ? $query->min('sort') - 1 : $query->max('sort') + 1;
     $this->owner->getDb()->createCommand()->insert($this->junctionTable, ['file_id' => $image->id, 'model' => $model, 'item_id' => $itemId, 'sort' => $sort])->execute();
     return true;
 }