欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

使用YII做后台,使用Yii的AR达不到要求的效果。很急,我该怎么办?

程序员文章站 2022-05-28 12:46:32
...
我使用YII作为PHP框架。其中有一个功能是查询附近的人。
我的代码是这样做的:
class NearByController extends Controller
{

    //发现附近的人。这里还没有做筛选。distance在哪里?
    public function  actionIndex()
    {
        $latitude=  UserRefreshForm::model()->getLatitude();
        $longitude=UserRefreshForm::model()->getLongitude();

        $criteria=new CDbCriteria();
        $nearPersons=MGetNear::getNearPersonsNoOrder();
        $criteria->select=' t.*, (ACOS(SIN(('.$latitude.'* 3.1415) / 180 ) *SIN((tbl_myr_refresh.latitude * 3.1415) / 180 ) +COS(('.$latitude.'* 3.1415) / 180 ) * COS((tbl_myr_refresh.latitude * 3.1415) / 180 ) *COS(('.$longitude .'* 3.1415) / 180 - (tbl_myr_refresh.longitude * 3.1415) / 180 ) ) * 6378.137)   as distance';
        $criteria->join='LEFT JOIN tbl_myr_refresh ON tbl_myr_refresh.userId=t.userId';
        $criteria->addInCondition('t.userId',$nearPersons);
        $criteria->order='distance ASC';
        $UserModel=User::model();
        $total=$UserModel->count($criteria);
        $pager=new CPagination($total);
        $pager->pageSize=10;
        $pager->applyLimit($criteria);
        $userList=$UserModel->findAll($criteria);
 p($userList);

    }

我是使用Mysql来计算距离的。
但是当我打印出来的时候发现没有distance这个属性:

Array
(
    [0] => User Object
        (
            [password1] => 
            [password2] => 
            [geohash] => 
            [_new:CActiveRecord:private] => 
            [_attributes:CActiveRecord:private] => Array
                (
                    [userId] => 3
                    [username] => 
                    [password] => c3284d0f94606de1fd2af172aba15bf3
                    [email] => 741691740@qq.com
                    [phoneNumber] => 
                    [birthday] => 1992-11-01
                    [gender] => 1
                    [homeland] => 四川南充
                    [wantWhere] => 
                    [registerDate] => 2014-06-27
                    [smallAvatar] => smallAvatar140385273350016.jpg
                    [bigAvatar] => bigAvatar140385273350016.jpg
                    [qrcode] => 
                    [isStudent] => 1
                    [wentWhere] => 
                    [md5] => f2300adcae381de30ae58c8f4309dda9
                    [sessionId] => o5fcapmo7clfiiua717n1aa525
                    [iemi] => 123456789
                )

            [_related:CActiveRecord:private] => Array
                (
                )

            [_c:CActiveRecord:private] => 
            [_pk:CActiveRecord:private] => 3
            [_alias:CActiveRecord:private] => t
            [_errors:CModel:private] => Array
                (
                )

            [_validators:CModel:private] => 
            [_scenario:CModel:private] => update
            [_e:CComponent:private] => 
            [_m:CComponent:private] => 
        )
)

也就是Yii只把user表中有的打印出来了。但是distance这个属性没有打印出来!
求救。YII大神告诉我该如何获得这个distance属性?

回复内容:

我使用YII作为PHP框架。其中有一个功能是查询附近的人。
我的代码是这样做的:

class NearByController extends Controller
{

    //发现附近的人。这里还没有做筛选。distance在哪里?
    public function  actionIndex()
    {
        $latitude=  UserRefreshForm::model()->getLatitude();
        $longitude=UserRefreshForm::model()->getLongitude();

        $criteria=new CDbCriteria();
        $nearPersons=MGetNear::getNearPersonsNoOrder();
        $criteria->select=' t.*, (ACOS(SIN(('.$latitude.'* 3.1415) / 180 ) *SIN((tbl_myr_refresh.latitude * 3.1415) / 180 ) +COS(('.$latitude.'* 3.1415) / 180 ) * COS((tbl_myr_refresh.latitude * 3.1415) / 180 ) *COS(('.$longitude .'* 3.1415) / 180 - (tbl_myr_refresh.longitude * 3.1415) / 180 ) ) * 6378.137)   as distance';
        $criteria->join='LEFT JOIN tbl_myr_refresh ON tbl_myr_refresh.userId=t.userId';
        $criteria->addInCondition('t.userId',$nearPersons);
        $criteria->order='distance ASC';
        $UserModel=User::model();
        $total=$UserModel->count($criteria);
        $pager=new CPagination($total);
        $pager->pageSize=10;
        $pager->applyLimit($criteria);
        $userList=$UserModel->findAll($criteria);
 p($userList);

    }

我是使用Mysql来计算距离的。
但是当我打印出来的时候发现没有distance这个属性:

Array
(
    [0] => User Object
        (
            [password1] => 
            [password2] => 
            [geohash] => 
            [_new:CActiveRecord:private] => 
            [_attributes:CActiveRecord:private] => Array
                (
                    [userId] => 3
                    [username] => 
                    [password] => c3284d0f94606de1fd2af172aba15bf3
                    [email] => 741691740@qq.com
                    [phoneNumber] => 
                    [birthday] => 1992-11-01
                    [gender] => 1
                    [homeland] => 四川南充
                    [wantWhere] => 
                    [registerDate] => 2014-06-27
                    [smallAvatar] => smallAvatar140385273350016.jpg
                    [bigAvatar] => bigAvatar140385273350016.jpg
                    [qrcode] => 
                    [isStudent] => 1
                    [wentWhere] => 
                    [md5] => f2300adcae381de30ae58c8f4309dda9
                    [sessionId] => o5fcapmo7clfiiua717n1aa525
                    [iemi] => 123456789
                )

            [_related:CActiveRecord:private] => Array
                (
                )

            [_c:CActiveRecord:private] => 
            [_pk:CActiveRecord:private] => 3
            [_alias:CActiveRecord:private] => t
            [_errors:CModel:private] => Array
                (
                )

            [_validators:CModel:private] => 
            [_scenario:CModel:private] => update
            [_e:CComponent:private] => 
            [_m:CComponent:private] => 
        )
)

也就是Yii只把user表中有的打印出来了。但是distance这个属性没有打印出来!
求救。YII大神告诉我该如何获得这个distance属性?

(ACOS(SIN(('.$latitude.'* 3.1415) / 180 ) *SIN((tbl_myr_refresh.latitude * 3.1415) / 180 ) +COS(('.$latitude.'* 3.1415) / 180 ) * COS((tbl_myr_refresh.latitude * 3.1415) / 180 ) *COS(('.$longitude .'* 3.1415) / 180 - (tbl_myr_refresh.longitude * 3.1415) / 180 ) ) * 6378.137) as distance

别名必须是该表中已经有的字段拿来借用一下,否则的话是row对象中获取不到这个值

相关标签: php yii