/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Image::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to return any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id]); $query->andFilterWhere(['like', 'name', $this->name])->andFilterWhere(['like', 'alt', $this->alt]); return $dataProvider; }
/** * @return \yii\db\ActiveQuery */ public function getImage() { return $this->hasOne(Image::className(), ['id' => 'image_id']); }
/** * Finds the Image model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Image the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Image::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
public function actionDeleteGalleryImage() { if (Yii::$app->request->isAjax and $id = Yii::$app->request->post('id')) { if ($model = Image::findOne($id)) { $model->delete(); unlink('images/products/galleries/' . $model->name); return true; } return false; } else { return $this->redirect(Yii::$app->request->referrer); } // if ($id = Yii::$app->request->post('image_id')) { // if ($model = Image::findOne($id)) { // $model->delete(); // unlink('images/products/galleries/'. $model->name); // return $this->redirect(Yii::$app->request->referrer); // } // } }
public function updateUpload() { $tokenImage = date("dmYHis") . bin2hex(openssl_random_pseudo_bytes(8)); if ($this->validate() and UploadedFile::getInstance($this, 'file')) { unlink('images/products/titles/' . $this->image); if ($this->file = UploadedFile::getInstance($this, 'file')) { $imageName = $this->file->baseName . '.' . $this->file->extension; $goodImage = $tokenImage . $imageName; $this->file->saveAs('images/products/titles/' . $goodImage); $this->image = $goodImage; } } // multiFiles upload if ($this->validate() and $this->multiFiles = UploadedFile::getInstances($this, 'multiFiles')) { foreach ($this->multiFiles as $file) { $goodImage = $tokenImage . $file->baseName . '.' . $file->extension; $file->saveAs('images/products/galleries/' . $goodImage); $image = new Image(); $image->name = $goodImage; $image->alt = $this->title; // $image->alt = $goodImage; $image->save(); $pi = new ProductImage(); $pi->product_id = $this->id; $pi->image_id = $image->id; $pi->save(); } } }