public function run() { $this->hotAlbum = Album::find()->limit(4)->all(); $this->hotDiary = Diary::find()->limit(10)->all(); $this->hotSong = Song::find()->limit(5)->orderBy(['created_at' => SORT_DESC])->all(); return $this->render('home-hot-content', array('hotAlbum' => $this->hotAlbum, 'hotDiary' => $this->hotDiary, 'hotSong' => $this->hotSong)); }
public function search($params) { $query = (new Query())->select('a.*')->addSelect('b.title as songKingdomTitle')->from(Song::tableName() . ' as a')->leftJoin(['b' => SongKingdom::tableName()], 'a.song_kingdom_id = b.id'); $this->load($params); if (!$this->validate()) { $query->where('0=1'); } else { $query->where('1=1'); $query->andFilterWhere(['a.id' => $this->id, 'a.song_kingdom_id' => $this->song_kingdom_id, 'a.song_time' => $this->song_time, 'a.liked_count' => $this->liked_count, 'a.fav_count' => $this->fav_count, 'a.share_count' => $this->share_count, 'a.download_count' => $this->download_count, 'a.comment_count' => $this->comment_count, 'a.play_count' => $this->play_count, 'a.created_by' => $this->created_by, 'a.created_at' => $this->created_at, 'a.updated_by' => $this->updated_by, 'a.updated_at' => $this->updated_at]); $query->andFilterWhere(['like', 'a.author', $this->author])->andFilterWhere(['like', 'a.title', $this->title])->andFilterWhere(['like', 'a.link', $this->link])->andFilterWhere(['like', 'a.label', $this->label])->andFilterWhere(['like', 'a.desc', $this->desc])->andFilterWhere(['like', 'a.singer', $this->singer])->andFilterWhere(['like', 'a.album', $this->album])->andFilterWhere(['like', 'a.privacy', $this->privacy])->andFilterWhere(['like', 'a.can_reply', $this->can_reply])->andFilterWhere(['like', 'a.icon', $this->icon])->andFilterWhere(['like', 'a.cover', $this->cover]); } $sql = $query->createCommand()->getRawSql(); $countQuery = $query->select('count(*)'); $count = $countQuery->createCommand()->queryScalar(); $defaultOrder = ['created_at' => SORT_DESC]; if (!empty($params['orderField'])) { $defaultOrder = [$params['orderField'] => $params['orderDirection'] == 'asc' ? SORT_ASC : SORT_DESC]; } $dataProvider = new SqlDataProvider(['sql' => $sql, 'totalCount' => $count, 'sort' => ['defaultOrder' => $defaultOrder, 'attributes' => array_values($this->attributes())], 'pagination' => ['pageSize' => 20, 'page' => isset($params['pageNum']) ? $params['pageNum'] - 1 : '0']]); return $dataProvider; }
/** * Finds the Song model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Song the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Song::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Displays a single SongKingdom model. * @param integer $id * @return mixed */ public function actionView($id) { $model = $this->findModel($id); return $this->render('songKingdomView', ['model' => $model, 'songModels' => Song::find()->andWhere(['song_kingdom_id' => $id])->all(), 'userCreadedBy' => User::find()->andWhere(['id' => $model->created_by])->asArray()->one(), 'userExtendCreadedBy' => UserExtend::find()->andWhere(['user_id' => $model->created_by])->asArray()->one(), 'user' => User::find()->andWhere(['id' => Yii::$app->getUser()->getId()])->asArray()->one(), 'userExtend' => UserExtend::find()->andWhere(['user_id' => Yii::$app->getUser()->getId()])->asArray()->one()]); }
/** * 保存创建和修改的信息 */ public function actionSave() { try { $post = Yii::$app->request->post(); $id = isset($post['song_id']) ? $post['song_id'] : ''; $post['Song']['song_kingdom_id'] = !empty($post['songKingdom_id']) ? $post['songKingdom_id'] : ''; if (!empty($id)) { $model = $this->findModel($id); if ($model->load($post) && $model->save()) { $song_id = $id; } } else { $model = new Song(); if ($model->load($post) && $model->save()) { $song_id = Yii::$app->db->getLastInsertID(); } } if (empty($song_id)) { throw new \Exception('写入歌曲失败'); } } catch (\Exception $exp) { $result = ['statusCode' => '300', 'message' => $exp->getMessage(), 'navTabId' => 'music_song_index_id', 'forwardUrl' => '', 'callbackType' => '']; echo Json::encode($result); exit; } $result = ['statusCode' => '200', 'message' => '操作成功', 'navTabId' => 'music_song_index_id', 'forwardUrl' => '', 'callbackType' => 'closeCurrent']; echo Json::encode($result); exit; }