在下面的代码中,我使用rest api列出了服务器场,并且它正在获取数据,但是该服务器配置文件模型与该用户模型相关,在fpro_userid中。我希望通过在farmprofile模型中返回fproUser来创建函数extrafields的用户表中的用户名。我在没有调用函数actionNearbyfarms()的情况下获取用户详细信息,但是如果调用该函数我没有收到用户详细信息。
任何人都可以帮忙吗?
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();
}
}
答案 0 :(得分:0)
使用$result = $command->queryAll()
;
在你的$ reuslt中你获得了所有的行,所以你只需要你应该访问的第一行(或唯一行)的列fpro_userid
$myActValue = $resuly[0]['fpro_userid'];
然后
return $model = FarmProfile::find()->
where(['fpro_userid' => $myActValue])->
andwhere(['fpro_practice_type' => $practicelist])->with('fproUser')->all();
或者你代替queryAll你应该使用queryScalar,它返回你选择命令的第一行的第一列
$result = $command->queryScalar();