public function actionCancel()
 {
     $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']])->one();
     $ans = Friend::findOne(['myid' => $myphone['id'], 'friendid' => $Fphone['id'], 'isfriend' => 0]);
     if ($ans) {
         $ans->delete();
         echo json_encode(array('flag' => 1, 'msg' => 'Cancel follow.'));
     } else {
         echo json_encode(array('flag' => 0, 'msg' => 'Do not exist.'));
     }
 }
 /**
  * Finds the Friend model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Friend the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Friend::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
 public function actionDelete()
 {
     $data = Yii::$app->request->post();
     $model = new User();
     $myid = $model->find()->select('id')->where(['phone' => $data['myphone']])->one();
     $fid = $model->find()->select('id')->where(['phone' => $data['fphone']])->one();
     $row1 = Friend::findOne(['myid' => $fid['id'], 'friendid' => $myid['id'], 'isfriend' => 1]);
     if ($row1 != null) {
         $row1->delete();
         $row2 = Friend::findOne(['myid' => $myid['id'], 'friendid' => $fid['id'], 'isfriend' => 1]);
         $row2->delete();
         echo json_encode(array('flag' => 1, 'msg' => 'Delete friend successfully'));
     } else {
         echo json_encode(array('flag' => 0, 'msg' => 'You are not friend.'));
     }
 }
 public function actionSearch()
 {
     $data = Yii::$app->request->post();
     Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
     $model = new User();
     $myid = $model->find()->select('id')->from('user')->where(['phone' => $data['myphone']])->one();
     $fid = $model->find()->select('*')->from('user')->where(['phone' => $data['fphone']])->one();
     $model = new Friend();
     // 		$info=$model->find()->where([
     // 				'myid'=>$myid['id'],
     // 				'friendid'=>$fid['id']
     // 		]);
     $info = Friend::findOne(['myid' => $myid['id'], 'friendid' => $fid['id']]);
     $ans = array();
     if ($fid) {
         $ans['id'] = $fid['id'];
         $ans['phone'] = $fid['phone'];
         $ans['thumb'] = $fid['thumb'];
         $ans['nickname'] = $fid['nickname'];
         $ans['gender'] = $fid['gender'];
         $ans['area'] = $fid['area'];
         $ans['job'] = $fid['job'];
         $ans['hobby'] = $fid['hobby'];
         $ans['signature'] = $fid['signature'];
         if ($info) {
             $ans['isfriend'] = 1;
         } else {
             $ans['isfriend'] = 0;
         }
     } else {
         $ans['id'] = '';
         $ans['phone'] = '';
         $ans['thumb'] = '';
         $ans['nickname'] = '';
         $ans['gender'] = '';
         $ans['area'] = '';
         $ans['job'] = '';
         $ans['hobby'] = '';
         $ans['signature'] = '';
         $ans['isfriend'] = 0;
     }
     return $ans;
 }