/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Collection::find(); // add conditions that should always apply here $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; } // grid filtering conditions $query->andFilterWhere(['id' => $this->id, 'collection_type_id' => $this->collection_type_id, 'public_option_id' => $this->public_option_id, 'membership_duration' => $this->membership_duration, 'member_count' => $this->member_count, 'manager_count' => $this->manager_count, 'sort_order' => $this->sort_order, 'status_id' => $this->status_id, 'created_at' => $this->created_at, 'updated_at' => $this->updated_at, 'created_by' => $this->created_by, 'updated_by' => $this->updated_by]); $query->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'alias', $this->alias])->andFilterWhere(['like', 'description', $this->description]); return $dataProvider; }
/** * 保存或删除一个收藏 * @param $userId * @param $testLibraryId * @return string * @throws Exception * @throws \Exception */ public static function saveOrDelete($userId, $testLibraryId) { $collection = Collection::find()->where(['userId' => $userId, 'testLibraryId' => $testLibraryId])->one(); if ($collection) { if (!$collection->delete()) { throw new Exception("Collection delete error"); } return "delete"; } else { $collection = new Collection(); $collection->userId = $userId; $collection->testLibraryId = $testLibraryId; $collection->createDate = DateFunctions::getCurrentDate(); if (!$collection->save()) { throw new Exception("Collection save error"); } return "collected"; } }