public function add($user_id, $place_id) { // check if it exists if (!UserPlace::find()->where(['user_id' => $user_id, 'place_id' => $place_id])->exists()) { // if not, add it $up = new UserPlace(); $up->user_id = $user_id; $up->place_id = $place_id; $up->save(); } }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = UserPlace::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'user_id' => $this->user_id, 'place_id' => $this->place_id, 'is_favorite' => $this->is_favorite, 'number_meetings' => $this->number_meetings, 'is_special' => $this->is_special, 'status' => $this->status, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['like', 'note', $this->note]); return $dataProvider; }
/** * @return \yii\db\ActiveQuery */ public function getUserPlaces() { return $this->hasMany(UserPlace::className(), ['place_id' => 'id']); }
<div class="meeting-place-form"> <?php $form = ActiveForm::begin(); ?> <?php echo $form->errorSummary($model); ?> <h3>Choose one of your places</h3> <div class="row"> <div class="col-md-6"> <?php echo Html::activeDropDownList($model, 'place_id', ArrayHelper::map(UserPlace::find()->all(), 'place.id', 'place.name'), ['prompt' => Yii::t('frontend', '-- select one of your places below --')]); ?> <h3>- or -</h3> <h3>Choose from Google Places</h3> <p>Type in a place or business known to Google Places:</p> <?php echo $form->field($model, 'searchbox')->textInput(['maxlength' => 255])->label('Place'); ?> </div> <div class="col-md-6"> <div id="map-canvas"> <article></article> </div> </div> </div> <!-- end row -->
/** * Finds the UserPlace model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return UserPlace the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = UserPlace::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }