public function actionAdd() { $model = new CommentModel(); $model->date = date("Y-m-d"); if ($model->load(Yii::$app->request->post()) && $model->save()) { $product = ProductModel::find()->where(['id' => $model->idProduct])->one(); $product->rating = ($product->rating * $product->amountRated + $model->mark) / ($product->amountRated + 1); $product->amountRated = $product->amountRated + 1; if (!$product->save()) { echo "Error updating product."; } } else { echo "Error adding comment."; } return $this->redirect(\Yii::$app->user->getReturnUrl()); }
/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = CommentModel::find(); $dataProvider = new ActiveDataProvider(['query' => $query]); if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'idUser' => $this->idUser, 'idProduct' => $this->idProduct, 'date' => $this->date, 'mark' => $this->mark]); $query->andFilterWhere(['like', 'text', $this->text]); return $dataProvider; }
public static function getUserCommentToProduct($productId, $userId) { $comments = \Yii::$app->cache->get('comments_product_' . $productId); if ($comments === false) { $comments = CommentModel::find()->where(['idProduct' => $productId])->all(); \Yii::$app->cache->add('comments_product_' . $productId, $comments); } foreach ($comments as $comment) { if ($comment->idUser == $userId) { return $comment; } } return null; }
public function actionCheckcomment() { $request = Yii::$app->request; // if(!$request->isAjax) return false; $id = intval($request->get('buff_id')); $filed_arr = ['id', 'buff_id', 'user_id', 'content', 'save_time' => 'time', 'nick_name' => 'username']; $data_thumb = CommentModel::find()->joinWith('business_customer')->where(['buff_id' => $id])->all(); // var_dump($data_thumb); foreach ($data_thumb as $k => $v) { $data_comment[$k] = $this->_checkAjaxInfo($v, $filed_arr); $data_id[$k] = $data_comment[$k]['id']; } //获取评论相关的回复信息 $data_reply = array(); $data_reply_thumb = ReplyModel::find()->joinWith('business_customer')->where(['comment_id' => $data_id])->all(); if (!$data_reply_thumb) { return json_encode('暂无评论'); } $filed_arr = ['id', 'comment_id' => 'com_id', 'user_id', 'content', 'p_user_id', 'p_reply_id', 'save_time', 'nick_name' => 'username']; foreach ($data_reply_thumb as $v) { $data_reply[] = $this->_checkAjaxInfo($v, $filed_arr); } $comment_info = $this->_CheckCommentInfo($data_comment, $data_reply); }
/** * Finds the CommentModel model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return CommentModel the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = CommentModel::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * @return \yii\db\ActiveQuery */ public function getComments() { return $this->hasMany(CommentModel::className(), ['idProduct' => 'id']); }