/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Building::find(); $query->select(['id', 'address', "ST_X(location)||','||ST_Y(location) as location"]); $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]); $query->andFilterWhere(['like', 'address', $this->address])->andFilterWhere(['like', 'location', $this->location]); return $dataProvider; }
private function openCompanies($count = 100) { print_r("Open companies. Count: " . $count . "..."); $this->faker->unique(true); $buildings = Building::find()->asArray()->all(); $building_ids = ArrayHelper::getColumn($buildings, 'id'); $building_ids_length = count($building_ids) - 1; for ($i = 0; $i < $count; $i++) { $company = new Company(); $company->title = $this->faker->unique()->company; $company->building_id = $building_ids[mt_rand(1, $building_ids_length)]; $company->save(); } print_r("DONE" . PHP_EOL); }
/** * Finds the Building model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Building the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Building::find()->select(['id', 'address', "ST_X(location)||','||ST_Y(location) as location"])->where(['id' => $id])->one()) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
use yii\helpers\ArrayHelper; /* @var $this yii\web\View */ /* @var $model app\models\Company */ /* @var $form yii\widgets\ActiveForm */ ?> <div class="company-form"> <?php $form = ActiveForm::begin(); ?> <?php echo $form->field($model, 'title')->textInput(['maxlength' => true]); ?> <?php echo $form->field($model, 'building_id')->dropDownList(ArrayHelper::map(\app\models\Building::find()->all(), 'id', 'address')); ?> <div class="form-group"> <?php echo Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-success' : 'btn btn-primary']); ?> </div> <?php ActiveForm::end(); ?> </div>
public function actionBuildings() { $buildings = Building::find()->select(['id', 'address', "ST_X(location)||','||ST_Y(location) as location"])->asArray()->all(); return $buildings; }
public function upgradeBuildingLevel($id) { $build = Building::find($id); $build->castle()->first()->calcCastleIncreaseResources(); $currentWood = $build->castle()->first()->getResources('wood'); $currentGold = $build->castle()->first()->getResources('gold'); $cost = $build->costUpdate(); if ($currentWood >= $cost && $currentGold >= $cost) { $build->levelUp(); $build->castle()->first()->subResource('wood', $cost); $build->castle()->first()->subResource('gold', $cost); return "success"; } else { return "no_costs"; } }