public function actionUploadImages() { $model = new TextBlockIamge(); if ($model->load(Yii::$app->request->post())) { $model->imageFiles = UploadedFile::getInstances($model, 'imageFiles'); if ($model->validate()) { foreach ($model->imageFiles as $file) { $filename = $file->baseName . rand(1001, 9009) . '.' . $file->extension; $savePathServer = dirname(__FILE__) . '/../../uploads/' . $filename; $file->saveAs($savePathServer); $newIamge = new TextBlockIamge(); $newIamge->path = $filename; $newIamge->text_block_id = $model->text_block_id; $newIamge->save(false); } return $this->redirect(['/text-block/view', 'id' => $model->text_block_id]); } } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = TextBlockIamge::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, 'ticket_block_id' => $this->ticket_block_id, 'created' => $this->created, 'updated' => $this->updated]); $query->andFilterWhere(['like', 'path', $this->path]); return $dataProvider; }
public function getImages() { return $this->hasMany(TextBlockIamge::classname(), ['text_block_id' => 'id']); }