how to Disambiguating same column name yii2

even yii master and expert did nt solve this,even yii core developer did nt solve this.

i have table user_device




id-----user_id-------device_id

1        1            10

2        1            11






and table position

id-----time----------------------device_id----------data

1      2015-05-20 12:00:00        10                 abc

2      2015-05-20 12:30:00        10                 xdf

3      2015-05-20 12:30:00        10                 xdf

4      2015-05-20 12:30:00        10                 xdf

5      2015-05-20 12:30:00        11                 xdf

6      2015-05-20 12:30:00        11                 xdf

7      2015-05-20 12:30:00        11                 xdf



now device is related to user and device is related to position table also

so i need data from position table but where it belongs to login user.

i have relation




public function getUserPosition()

    {

        return $this->hasMany(Positions::className(), ['device_id' => 'device_id']);

    }



it should look like below




[0] => Array

        (

            [id] => 356114

            [user_id] => 1

            [device_id] => 10       //this is user_device data

            [status] => 

            [is_deleted] => 

               [userPosition] => Array

                (

                    [0] => Array

                        (

                            [id] => 356114

                            [device_id] => 10              //this is position data

                            [data] => abc

                            [time] => 2015-05-20 15:07:00

                          )

                     [1] => Array

                        (

                            [id] => 356114

                            [device_id] => 10

                            [data] => abc

                            [time] => 2015-05-20 15:07:34

                          )

                     [......


                   .....................

                 )

 )

[1] => Array

        (

            [id] => 356114

            [user_id] => 1

            [device_id] => 11

            [status] => 

            [is_deleted] => 

               [userPosition] => Array

                (

                    [0] => Array

                        (

                            [id] => 356114

                            [device_id] => 11

                            [data] => abc

                            [time] => 2015-05-20 15:07:00

                          )

                     [1] => Array

                        (

                            [id] => 356114

                            [device_id] => 11

                            [data] => abc

                            [time] => 2015-05-20 15:07:34

                          )

                     [......


                   .....................

                 )

 )



i need data from position table but where user_id is 1 and time between start_time and end_time and first need is device data from user_device and below subarray of realted data of device from position

may be batch query have to use.

see link

im not properly understand ur requirement.

what i have done.

@UserdeviceSearch


$query = Userdevice::find();

        $query->joinWith(['deviceposition']);

@Userdevice


public function getDeviceposition()

       {

        return $this->hasMany(Position::className(), ['device_id' => 'device_id']);

        }

i need this with relation so this will come like above format ,with yii query builder it is giving me in one array

i need like below so related data comes into another array




[0] => Array

        (

            [id] => 356114

            [user_id] => 1

            [device_id] => 10       //this is user_device data

            [status] => 

            [is_deleted] => 

               [userPosition] => Array

                (

                    [0] => Array

                        (

                            [id] => 356114

                            [device_id] => 10              //this is position data

                            [data] => abc

                            [time] => 2015-05-20 15:07:00

                          )

                     [1] => Array

                        (

                            [id] => 356114

                            [device_id] => 10

                            [data] => abc

                            [time] => 2015-05-20 15:07:34

                          )

                     [......


                   .....................

                 )

 )



i m getting this data have a look in attached.

is this your requ.?


$query = Userdevice::find();

        $query->joinWith(['deviceposition'])->asArray();

i think you did nt get me

i used this query according to u it is giving me object and i need array

see below




yii\db\ActiveQuery Object

(

    [sql] => 

    [on] => 

    [joinWith] => 

    [select] => Array

        (

            [0] => speed

            [1] => latitude

            [2] => longitude

        )


    [selectOption] => 

    [distinct] => 

    [from] => Array

        (

            [0] => user_device

        )


    [groupBy] => 

    [join] => Array

        (

            [0] => Array

                (

                    [0] => LEFT JOIN

                    [1] => positions

                    [2] => {{user_device}}.[[device_id]] = {{positions}}.[[device_id]]

                )


        )


    [having] => 

    [union] => 

    [params] => Array

        (

        )


    [_events:yii\base\Component:private] => Array

        (

        )


    [_behaviors:yii\base\Component:private] => Array

        (

        )


    [where] => time BETWEEN "2015-05-15 00:00:00" AND "2015-05-15 23:59:59" AND user_id= 127 AND speed > 0

    [limit] => 

    [offset] => 

    [orderBy] => 

    [indexBy] => 

    [modelClass] => app\models\UserDevice

    [with] => Array

        (

            [0] => deviceposition

        )


    [asArray] => 1

    [multiple] => 

    [primaryModel] => 

    [link] => 

    [via] => 

    [inverseOf] => 

)



i m using this query




 $query = Userdevice::find();

        $query->joinWith(['deviceposition'])->asArray();

        $query->select(['speed','lat','long']);

       $query->where('time BETWEEN "'.$start_time.'" AND "'.$end_time.'" AND user_id= 1 ');



are where are u printing this query? in same search model?

print in view/index.php

like


echo "<pre>";

 print_r($dataProvider->models);exit;

@UserdeviceSearch in model : same data i got like this.




 $query = Userdevice::find()->joinWith(['deviceposition'])->asArray()->all();


echo "<pre>";

 print_r($query);exit;

$query = Userdevice::find();

    &#036;query-&gt;joinWith(['deviceposition']);


    &#036;query-&gt;select(['speed','lat','long']);


   &#036;query-&gt;where('time BETWEEN &quot;'.&#036;start_time.'&quot; AND &quot;'.&#036;end_time.'&quot; AND user_id= 1 ')[color=&quot;#FF0000&quot;]-&gt;asArray()-&gt;all()[/color];

simple use this in one line.

[color="#FF0000"]$query = Userdevice::find()->joinWith([‘deviceposition’])->select([‘speed’,‘lat’,‘long’])->where(‘time BETWEEN "’.$start_time.’" AND “’.$end_time.’” AND user_id= 1 ')->asArray()->all();[/color]




Array

(

    [0] => Array

        (

            [id] => 101

            [user_id] => 108

            [device_id] => 49

            [created_date] => 2015-05-01

            [expiry_date] => 2019-12-31

            [status] => 

            [is_deleted] => 

            [deviceposition] => Array

                (

                    [0] => Array

                        (

                            [id] => 308316

                            [device_id] => 49

                            [speed] => 0

                            [time] => 2015-05-01 11:33:57

                           

                        )