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

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

WBOY
WBOYOriginal
2016-06-06 20:44:511115Durchsuche

我使用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对象中获取不到这个值

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn