/** * Displays a single Site model. * @param integer $id * @return mixed */ public function actionView($id) { Yii::$app->response->format = 'json'; $model = Site::find()->where(['site.id' => $id])->select(['site.*', 'package.*', 'material.*'])->innerJoin('location_history')->innerJoin('package')->innerJoin('material')->asArray()->one(); // $response = []; $model = Site::find()->where(['site.id' => $id])->asArray()->one(); $packages = LocationHistory::find()->where(['site_id' => $model['id']])->orderBy('timestamp DESC')->groupBy('package_id')->asArray()->all(); $response = $model; $response['materials'] = []; foreach ($packages as $key => $value) { $package = Package::find()->with('material')->asArray()->one(); if (isset($response['materials'][strtolower($package['material']['name'])])) { $response['materials'][strtolower($package['material']['name'])] = ['weight' => $response['materials'][strtolower($package['material']['name'])] + $package['weight']]; } else { $response['materials'][strtolower($package['material']['name'])] = ['weight' => $package['weight']]; } } return $response; }
/** * Lists all LocationHistory models. * @return mixed */ public function actionIndex() { $dataProvider = new ActiveDataProvider(['query' => LocationHistory::find()]); return $this->render('index', ['dataProvider' => $dataProvider]); }
/** * Finds the LocationHistory model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return LocationHistory the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = LocationHistory::find()->where(['id' => $id])->with('package', 'site')->asArray()->one()) !== null) { return $model; } else { Yii::$app->response->format = 'json'; Yii::$app->response->setStatusCode(404); Yii::$app->response->data = ['message' => 'Record not found']; Yii::$app->response->send(); } }