public function actionRequestadd()
 {
     $data = Yii::$app->request->post();
     $model1 = new User();
     $myid = $model1->find()->select('id')->where(['phone' => $data['myphone']])->one();
     $fid = $model1->find()->select('id')->where(['phone' => $data['fphone']])->one();
     if (!$myid) {
         echo json_encode(array('flag' => 0, 'msg' => 'my phone do not exist!'));
         return;
     }
     if (!$fid) {
         echo json_encode(array('flag' => 0, 'msg' => 'friend phone do not exist!'));
         return;
     }
     $model = new Reqfriend();
     $model->myid = $myid['id'];
     $model->friendid = $fid['id'];
     $row = Reqfriend::findOne(['myid' => $myid['id'], 'friendid' => $fid['id']]);
     if ($row != null) {
         $row->delete();
     }
     $friend = new Friend();
     $num = $friend->find()->andWhere(['myid' => $myid['id'], 'friendid' => $fid['id']])->count();
     if ($num == 0) {
         $model->save();
         echo json_encode(array('flag' => 1, 'msg' => 'ReqSuccessfully'));
     } else {
         echo json_encode(array('flag' => 0, 'msg' => 'Alreadyexists'));
     }
 }
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     //$query = Friend::find()->join('INNER JOIN','user','friends.friendid=user.id');
     $this->load($params);
     if (isset($params['FollowSearch']['mynickname']) && !empty($params['FollowSearch']['mynickname'])) {
         $query = Friend::find()->join('INNER JOIN', 'user', 'friends.myid=user.id');
         $query->andFilterWhere(['like', 'user.nickname', $params['FollowSearch']['mynickname']]);
     } else {
         $query = Friend::find()->join('INNER JOIN', 'user', 'friends.friendid=user.id');
     }
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $query->andFilterWhere(['friends.isfriend' => 0]);
     if ($params != false && !empty($params['FollowSearch'])) {
         //$b=$a;
         //=app::find()->where("name= :name",[':name'=>'QQ'])->one();
         //if()
         foreach ($params['FollowSearch'] as $name => $value1) {
             if ($name === 'myid' && $value1 != null) {
                 $appinfo = User::findOne(['phone' => $params['FollowSearch']['myid']]);
                 $this->value = $appinfo['id'];
                 if ($appinfo == null) {
                     $this->value = 0;
                 }
             }
             if ($name === 'friendid' && $value1 != null) {
                 $appinfo = User::findOne(['phone' => $params['FollowSearch']['friendid']]);
                 $this->userinc = $appinfo['id'];
                 if ($appinfo == null) {
                     $this->userinc = 0;
                 }
             }
         }
     }
     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(['myid' => $this->value, 'friendid' => $this->userinc]);
     if (isset($params['FollowSearch']) && isset($params['FollowSearch']['friendnickname'])) {
         $query->andFilterWhere(['like', 'user.nickname', $params['FollowSearch']['friendnickname']]);
     }
     return $dataProvider;
 }
 public function actionSet()
 {
     $data = Yii::$app->request->post();
     $model = new Friend();
     $user = new User();
     $myphone = $user->find()->select('id')->where(['phone' => $data['myphone']])->one();
     $Fphone = $user->find()->select('id')->where(['phone' => $data['fphone'], 'famous' => 1])->one();
     if (!$Fphone) {
         echo json_encode(array('flag' => 0, 'msg' => 'Failed!He/She is not famous.'));
         return;
     }
     if ($model->find()->where(['myid' => $myphone['id'], 'friendid' => $Fphone['id']])->one()) {
         echo json_encode(array('flag' => 0, 'msg' => 'Failed!You have already followed or you are friend.'));
     } else {
         $model->myid = $myphone['id'];
         $model->friendid = $Fphone['id'];
         $model->isfriend = 0;
         $model->save();
         echo json_encode(array('flag' => 1, 'msg' => 'Follow success!'));
     }
 }