public function search($params) { $query = MChannel::find(); // method #1 // $query->with('fans'); // method #2 // $query->with(['fans'=>function($query) { $query->andWhere('subscribe=1'); }]); // method #3 $subQuery = MUser::find()->select('gh_id as gh_id_x, scene_pid as scene_pid_x, count(*) as fans_cnt')->where('scene_pid!=0')->groupBy(['gh_id', 'scene_pid']); $query->leftJoin(['wx_user' => $subQuery], 'gh_id=gh_id_x AND scene_id = scene_pid_x'); $dataProvider = new ActiveDataProvider(['query' => $query, 'sort' => ['defaultOrder' => [], 'attributes' => ['title', 'mobile', 'fansCount' => ['asc' => ['wx_user.fans_cnt' => SORT_ASC], 'desc' => ['wx_user.fans_cnt' => SORT_DESC]]]], 'pagination' => ['pageSize' => 20]]); /* if (Yii::$app->user->identity->gh_id == 'root') throw new NotFoundHttpException("Please selected one gh_id for the root first!"); else if (Yii::$app->user->identity->openid == 'admin') { $this->gh_id = Yii::$app->user->identity->gh_id; $this->addCondition($query, 'gh_id'); } */ if (!($this->load($params) && $this->validate())) { return $dataProvider; } $this->addCondition($query, 'id'); $this->addCondition($query, 'title', true); $this->addCondition($query, 'mobile', true); $query->andFilterWhere(['wx_user.fans_cnt' => $this->fansCount]); return $dataProvider; }
public function actionCreateChannelQrs() { $gh_id = Yii::$app->wx->getGhid(); $file = Yii::$app->getRuntimePath() . DIRECTORY_SEPARATOR . 'channel_names.txt'; $fh = fopen($file, "r"); $i = 0; $scene_ids = array(); while (!feof($fh)) { $line = fgets($fh); if (empty($line)) { continue; } $title = trim($line); $model = MChannel::findOne(['gh_id' => $gh_id, 'title' => $title]); if ($model !== null) { continue; } $model = new MChannel(); $model->gh_id = $gh_id; $model->title = $title; $url = $model->getQrImageUrl(); $i++; // if ($i > 2) break; } fclose($fh); }
public static function getChannelScoreTopx($gh_id, $date_start, $date_end) { $key = md5(serialize([__METHOD__, $gh_id, $date_start, $date_end])); $value = Yii::$app->cache->get($key); if ($value !== false) { return $value; } $channels = MChannel::findAll(['gh_id' => $gh_id]); $rows = []; foreach ($channels as $channel) { $row = []; $row['id'] = $channel->id; $row['title'] = $channel->title; $row['cnt_sum'] = $channel->getScoreFromLogRange($date_start, $date_end); $rows[] = $row; } Yii::$app->cache->set($key, $rows, YII_DEBUG ? 10 : 12 * 3600); return $rows; }
public function getChannel() { return $this->hasOne(MChannel::className(), ['gh_id' => 'gh_id', 'openid' => 'openid']); }