/**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = ShortLinks::find();
     $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => ['id' => SORT_DESC]]]);
     if (!($this->load($params) && $this->validate())) {
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'count_click' => $this->count_click, 'field_id' => $this->field_id, 'user_id' => $this->user_id, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at]);
     $query->andFilterWhere(['like', 'link', $this->link])->andFilterWhere(['like', 'short', $this->short])->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'table', $this->table]);
     return $dataProvider;
 }
예제 #2
0
 public static function findByToken($token)
 {
     $model = ShortLinks::find()->where(['token' => $token])->one();
     if ($model) {
         $click = new ShortLinksClick();
         $click->short_links_id = $model->id;
         if (!$click->save()) {
             Yii::error(['msg' => 'Ошибка записи статистики клика', 'data' => ['error' => $model->errors, 'method' => __METHOD__]]);
         }
         $model->count_click += 1;
         $model->save();
         // Запишем ссылку в куки
         $cookies = Yii::$app->response->cookies;
         // добавление новой куки в HTTP-ответ
         $cookies->add(new \yii\web\Cookie(['name' => 'short_link_id', 'value' => $model->id, 'expire' => time() + 60 * 60 * 24 * 7]));
         return $model->link;
     }
     return false;
 }
 /**
  * Finds the ShortLinks model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return ShortLinks the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = ShortLinks::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
 /**
  * @return \yii\db\ActiveQuery
  */
 public function getShortLinks()
 {
     return $this->hasOne(ShortLinks::className(), ['id' => 'short_links_id']);
 }