/** * 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; }
/** * @return \yii\db\ActiveQuery */ public function getCurrentLocation() { return $this->hasOne(LocationHistory::className(), ['package_id' => 'id'])->orderBy('timestamp DESC')->with('site')->groupBy('package_id'); }
public function getMaterials() { return $this->hasMany(LocationHistory::className(), ['site_id' => 'id'])->select('location_history.*')->innerJoin('package')->innerJoin('material'); }
/** * 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::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * 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(); } }