/** * Creates a new Expenses model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Expenses(); if ($model->load(Yii::$app->request->post())) { $model->loadDefaultValues(); $model->save(); if ($model->save()) { $exp = new EntpExpenses(); $exp->expenses_id = $this->id; $exp->entrepreneur_id = Yii::$app->user->id; $exp->link('expenses', $model); } Yii::$app->session->setFlash('success', 'ok'); $searchModel = new EntpExpensesSearch(); $dataProvider = $searchModel->search(Yii::$app->request->queryParams); return $this->render('index', ['searchModel' => $searchModel, 'dataProvider' => $dataProvider]); //return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $id = Yii::$app->user->id; $query = EntpExpenses::find(); $query->joinWith(['entrepreneur', 'expenses'])->where('entp_expenses.entrepreneur_user_id=' . $id); $dataProvider = new ActiveDataProvider(['query' => $query]); $dataProvider->sort->attributes['total'] = ['asc' => ['expenses.total' => SORT_ASC], 'desc' => ['expenses.total' => SORT_DESC]]; $dataProvider->sort->attributes['reason'] = ['asc' => ['expenses.reason' => SORT_ASC], 'desc' => ['expenses.reason' => SORT_DESC]]; $dataProvider->sort->attributes['created_at'] = ['asc' => ['expenses.created_at' => SORT_ASC], 'desc' => ['expenses.created_at' => SORT_DESC]]; $dataProvider->sort->attributes['updated_at'] = ['asc' => ['expenses.updated_at' => SORT_ASC], 'desc' => ['expenses.updated_at' => SORT_DESC]]; $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(['entrepreneur_user_id' => $this->entrepreneur_user_id, 'expenses_id' => $this->expenses_id]); $query->andFilterWhere(['like', 'expenses.total', $this->total])->andFilterWhere(['like', 'expenses.reason', $this->reason])->andFilterWhere(['like', 'expenses.created_at', $this->created_at])->andFilterWhere(['like', 'expenses.updated_at', $this->updated_at]); return $dataProvider; }
/** * @return \yii\db\ActiveQuery */ public function getEntpExpenses() { return $this->hasMany(EntpExpenses::className(), ['entrepreneur_id' => 'id']); }
/** * Finds the EntpExpenses model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $entrepreneur_id * @param integer $expenses_id * @return EntpExpenses the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($entrepreneur_user_id, $expenses_id) { if (($model = EntpExpenses::findOne(['entrepreneur_user_id' => $entrepreneur_user_id, 'expenses_id' => $expenses_id])) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }