-1

In the below code I am listing the farms using a rest api, and it is getting the data, but the farm profile model has related to the user model, in that fpro_userid. I want the username from the user table that I created a function extrafields by return the fproUser in farmprofile model. I am getting the user details without calling the function actionNearbyfarms(), but if call that function I am not getting the user details.

Can any one help?

public function actionNearbyfarms()
{
    $practice =  Yii::$app->request->getQueryParam('practice');
    $practicelist = "";
    if ($practice != null && $practice != "") {
        $practicelist = FarmProfile::find()->where('fpro_practice_type='.$practice)->all();          

    }

    $user_lat =  Yii::$app->request->get('ip_lat')?Yii::$app->request->get('ip_lat'):'13.0973700000';
    $user_lan =  Yii::$app->request->get('ip_lan')?Yii::$app->request->get('ip_lan'):'77.3952590000';

    $nearbylocations = array();
    $nearbyfarms = array();

    $connection = Yii::$app->getDb();

        $command = $connection->createCommand('
            SELECT fpro_userid, 6371 * 2 * ASIN(SQRT( POWER(SIN(('.$user_lat.' - abs(fpro_lat)) * pi()/180 / 2),2) + COS('.$user_lan.' * pi()/180 ) * COS( abs(fpro_lan) * pi()/180) * POWER(SIN(('.$user_lan.' - fpro_lan) * pi()/180 / 2), 2) )) as distance FROM farm_profile having distance < 30 ORDER BY distance
            ');  

    $result = $command->queryAll();

    if($practice != null && $practice != ""){
        return $model = FarmProfile::find()->where(['fpro_userid' => $result])->andwhere(['fpro_practice_type' => $practicelist])->with('fproUser')->all();
    } else {
        return $model = FarmProfile::find()->where(['fpro_userid' => $result])->with('fproUser')->all();
    }
}
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Pradeep Deepu
  • 25
  • 1
  • 3

1 Answers1

0

Using $result = $command->queryAll() ;

In your $reuslt you obtain all the rows so you need just the column fpro_userid of the first (or unique rows) you should accessi

  $myActValue = $resuly[0]['fpro_userid'];

and then

 return $model = FarmProfile::find()->
           where(['fpro_userid' => $myActValue])->
           andwhere(['fpro_practice_type' => $practicelist])->with('fproUser')->all();

or you instead of queryAll you should use queryScalar, that return the first column of the firts row of you select command

  $result = $command->queryScalar();
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107