/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Unit::find(); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['unit_id' => SORT_ASC], 'enableMultiSort' => true]]); $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(['unit_id' => $this->unit_id]); $query->andFilterWhere(['like', 'unit_text', $this->unit_text]); return $dataProvider; }
/** * Deletes an existing Amount model. * If deletion is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed */ public function actionDelete($id) { $main_data = \backend\models\Main::find()->select(['amount_id'])->andWhere('amount_id = :id', [':id' => $id])->groupBy('amount_id')->one(); $model = $this->findModel($id); // Look-up Unit $unit_data = \backend\models\Unit::find()->select(['unit_text'])->andWhere('unit_id = :unit_id', [':unit_id' => $model->unit_id])->one(); if (empty($main_data->amount_id)) { if (file_exists($model->amount_img)) { unlink($model->amount_img); } $model->delete(); Yii::$app->getSession()->setFlash('amount_delete_success', ['type' => Growl::TYPE_SUCCESS, 'title' => '<b>' . Yii::t('backend', 'Success') . '</b>', 'icon' => 'glyphicon glyphicon-ok-sign', 'body' => '<strong>' . $model->amount_text . ' ' . $unit_data->unit_text . '</strong> ' . Yii::t('backend', 'has been deleted.')]); } else { Yii::$app->getSession()->setFlash('amount_delete_failed', ['type' => \kartik\growl\Growl::TYPE_DANGER, 'title' => '<b>' . Yii::t('backend', 'Error') . '</b>', 'icon' => 'glyphicon glyphicon-remove-sign', 'body' => '<strong>' . $model->amount_text . ' ' . $unit_data->unit_text . '</strong> ' . Yii::t('backend', 'is currently being used and cannot be deleted.')]); } return $this->redirect(['index']); }
/** * @return \yii\db\ActiveQuery */ public function getUnit() { return $this->hasOne(Unit::className(), ['unit_id' => 'unit_id']); }
/** * Finds the Unit model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Unit the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Unit::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }