public function updatePhotoCount($id) { $quuery = Photo::find()->select('count(1) as photo_count')->andWhere(['album_id' => $id])->asArray()->one(); $model = Album::findOne($id); $model->photo_count = $quuery['photo_count']; $model->save(); }
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)); }
/** * Displays a single Photo model. * @param integer $id * @return mixed */ public function actionView($id) { $model = $this->findModel($id); $albumModel = Album::findOne($model->album_id); $photoCount = Photo::find()->andWhere(['album_id' => $model->album_id])->count('*'); $currentPage = Photo::find()->andWhere(['album_id' => $model->album_id])->andWhere('id >= ' . $id)->count('*'); return $this->render('photoView', ['model' => $model, 'photoCount' => $photoCount, 'currentPage' => $currentPage, 'albumModel' => $albumModel, '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()]); }
/** * 个人相册 * @param $userId * @return array */ public function personalAlbumByUserId($userId) { $query = Album::find()->andWhere(['created_by' => $userId]); $countQuery = clone $query; $pages = new Pagination(['totalCount' => $countQuery->count()]); $pages->setPageSize(10); $models = $query->offset($pages->offset)->limit($pages->limit)->all(); return ['models' => $models, 'pages' => $pages, 'userCreadedBy' => User::find()->andWhere(['id' => $userId])->asArray()->one(), 'userExtendCreadedBy' => UserExtend::find()->andWhere(['user_id' => $userId])->asArray()->one()]; }
public function search($params) { $query = (new Query())->select('a.*')->addSelect('b.title as albumTitle')->from(Photo::tableName() . ' as a')->leftJoin(['b' => Album::tableName()], 'a.album_id = b.id'); $this->load($params); if (!$this->validate()) { $query->where('0=1'); } else { $query->where('1=1'); $query->andFilterWhere(['id' => $this->id, 'album_id' => $this->album_id, 'recs_count' => $this->recs_count, 'liked_count' => $this->liked_count, 'prev_photo' => $this->prev_photo, 'next_photo' => $this->next_photo, 'created_by' => $this->created_by, 'created_at' => $this->created_at, 'updated_by' => $this->updated_by, 'updated_at' => $this->updated_at]); $query->andFilterWhere(['like', 'alt', $this->alt])->andFilterWhere(['like', 'author', $this->author])->andFilterWhere(['like', 'title', $this->title])->andFilterWhere(['like', 'icon', $this->icon])->andFilterWhere(['like', 'cover', $this->cover])->andFilterWhere(['like', 'thumb', $this->thumb])->andFilterWhere(['like', 'image', $this->image])->andFilterWhere(['like', 'privacy', $this->privacy])->andFilterWhere(['like', 'can_reply', $this->can_reply])->andFilterWhere(['like', 'desc', $this->desc])->andFilterWhere(['like', 'is_album_cover', $this->is_album_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 Album model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Album the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Album::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * 保存创建和修改的信息 */ public function actionSave() { try { $post = Yii::$app->request->post(); $id = isset($post['album_id']) ? $post['album_id'] : ''; if (!empty($id)) { $model = $this->findModel($id); if ($model->load($post) && $model->save()) { $album_id = $id; } } else { $model = new Album(); if ($model->load($post) && $model->save()) { $album_id = Yii::$app->db->getLastInsertID(); } } if (empty($album_id)) { throw new \Exception('写入相册失败'); } $model->updatePhotoCount($album_id); } catch (\Exception $exp) { $message = ''; $message .= $exp->getMessage(); $result = ['statusCode' => '300', 'message' => $message, 'navTabId' => 'photo_album_index_id', 'forwardUrl' => '', 'callbackType' => '']; echo Json::encode($result); exit; } $result = ['statusCode' => '200', 'message' => '操作成功', 'navTabId' => 'photo_album_index_id', 'forwardUrl' => '', 'callbackType' => 'closeCurrent']; echo Json::encode($result); exit; }
public function getUserLable($userId) { // TODO: Implement getUserLable() method. return Album::find()->andWhere(['created_by' => $userId])->all(); }
/** * 保存后进行处理 * 写入照片的上一页,下一页 * * @param bool $insert * @param array $changedAttributes */ public static function updatePrevNextAndCover($album_id) { if (!empty($album_id)) { $photos = Photo::find()->andWhere(['album_id' => $album_id])->all(); $albumCoverPhoto = null; if (!empty($photos)) { foreach ($photos as $key => $val) { if ($val->is_album_cover == 1) { $albumCoverPhoto = $val; } $prev_photo = Photo::getPhotoHasPrev($key, $photos); $next_photo = Photo::getPhotoHasNext($key, $photos); if ($prev_photo == $val->prev_photo && $next_photo == $val->next_photo) { continue; } else { $Photo = Photo::findOne($val->id); $Photo->prev_photo = $prev_photo; $Photo->next_photo = $next_photo; $Photo->save(); } } } if (empty($albumCoverPhoto)) { $albumCoverPhoto = array_shift($photos); } $Album = Album::findOne($album_id); $Album->image = $albumCoverPhoto->image; $Album->cover = $albumCoverPhoto->cover; $Album->thumb = $albumCoverPhoto->thumb; $Album->icon = $albumCoverPhoto->icon; $Album->save(); } }
/** * Updates an existing Photo model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); $albumModel = Album::findOne($model->album_id); return $this->render('update', ['model' => $model, 'albumModel' => $albumModel]); }