首頁 >後端開發 >php教程 >使用YII做后台,使用Yii的AR达不到要求的效果。很急,我该怎么办?

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

WBOY
WBOY原創
2016-06-06 20:44:511128瀏覽

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

<code class="lang-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);

    }
</code>

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

<code class="lang-php">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] => 
        )
)
</code>

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

回复内容:

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

<code class="lang-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);

    }
</code>

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

<code class="lang-php">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] => 
        )
)
</code>

也就是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对象中获取不到这个值

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn