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

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

WBOY
Release: 2016-06-06 20:44:51
Original
1131 people have browsed it

我使用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>
Copy after login
Copy after login

我是使用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>
Copy after login
Copy after login

也就是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>
Copy after login
Copy after login

我是使用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>
Copy after login
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template