/** * Creates data provider instance with search query applied * * @param array $params * * @return ActiveDataProvider */ public function search($params) { $query = Messages::find(); $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; } $query->andFilterWhere(['id' => $this->id, 'userid' => $this->userid, 'created_at' => $this->created_at]); $query->andFilterWhere(['like', 'content', $this->content])->andFilterWhere(['like', 'pictures', $this->pictures]); return $dataProvider; }
/** * @return \yii\db\ActiveQuery */ public function getMessage() { return $this->hasOne(Messages::className(), ['id' => 'messageid']); }
public function actionDelete() { $data = Yii::$app->request->post(); $id = $data['messageid']; $msg = new Messages(); $user = Users::findOne(['phone' => $data['phone']]); $msg = Messages::find()->where(['id' => $id])->one(); if ($msg == null) { return array('flag' => 0, 'msg' => 'Message do not exist!'); } if ($user['id'] != $msg['userid']) { return array('flag' => 0, 'msg' => 'you do not own the message!'); } if ($msg->delete()) { return array('flag' => 1, 'msg' => 'Delete success!'); } else { return array('flag' => 0, 'msg' => 'Delete failed!'); } }
/** * Finds the Messages model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Messages the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Messages::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }