/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params, $locationIds = null) { $query = Location::find(); if (!is_null($locationIds) && is_array($locationIds)) { $query->where(['id' => $locationIds]); } $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; } $dataProvider->setSort(['attributes' => ['id', 'name', 'type_id']]); $query->andFilterWhere(['id' => $this->id, 'type_id' => $this->type_id]); $query->andFilterWhere(['like', 'name', $this->name]); return $dataProvider; }
/** * @return \yii\db\ActiveQuery */ public function getLocation() { return $this->hasOne(Location::className(), ['id' => 'location_id']); }
/** * @return \yii\db\ActiveQuery */ public static function findLocation($input) { if (!is_array($input)) { return null; } $listTypes = ArrayHelper::map(static::$types, 'tag', 'id'); $filtered = []; foreach ($input as $inputKey => $inputValue) { if (isset($listTypes[$inputKey])) { $filtered[$inputKey] = $inputValue; } } if (empty($filtered)) { return null; } $counter = 0; $locationTable = Location::tableName(); $query = static::find(); foreach ($filtered as $type => $name) { if ($counter === 0) { $query->andWhere(['and', [$locationTable . '.type_id' => $listTypes[$type]], [$locationTable . '.name' => $name]]); } else { $linksTable = 'link' . strval($counter); $query->leftJoin(LocationLinks::tableName() . ' AS ' . $linksTable, $linksTable . '.lower_id = ' . $locationTable . '.id'); $locationTable = 'loc' . strval($counter); $query->leftJoin(Location::tableName() . ' AS ' . $locationTable, $locationTable . '.id = ' . $linksTable . '.upper_id'); $query->andWhere(['and', [$locationTable . '.type_id' => $listTypes[$type]], [$locationTable . '.name' => $name]]); } $counter++; } // $result = $query->all(); // if( is_array($result) && (count($result) == 1) ) return $result[0]; // return $result; return $query; }