Yii2: subquery within model relation

From my three models/tables:

-Orders (id | name | comments | ...)
-OrdersLines (id | orderID | name | sDate | eDate | comments | ...)
-OrdersLinesStats (id | lineID | date | status | ...)

I need to get the last entered record from OrdersLinesStats for a given array of lineIDs (the ones in the hasMany relation) which can be done with this mysql subquery:

SELECT * FROM 
    (SELECT * FROM `ordersLinesStats` 
        WHERE `lineID` IN (1873, 1872, 1884, 1883, 1870, 1874, 1876, 1880, 1871, 1877, 1881, 1882, 1885, 1886, 1869, 1875, 1878)
        ORDER BY `ordersLinesStats`.`id` DESC
    )
    AS lastStats GROUP BY `lineID`

so, in OrdersLines model I created below method

public function getLastLineStats()
    {
        $subQuery = (new Query())
            ->select('*')
            ->from('ordersLinesStats')
            ->where(['lineID' => 'id'])
            ->orderBy([
                    'id' => SORT_DESC
                ]);
        return $this->hasMany(OrdersLinesStats::className(), ['lineID' => 'id'])
            ->from(['lastLineStats' => $subQuery])
            ->groupBy('lineID');
    }

and in Orders model i have:

public function getOrdersLines()
    {
        return $this
            ->hasMany(OrdersLines::className(), ['orderID' => 'id'])
            ->orderBy(['typeID' => SORT_ASC, 'name' => SORT_ASC])
            ->with(['lastLineStats']);
    }

but the resulting query when getting $model->ordersLines is this:

SELECT * FROM 
(SELECT * FROM `ordersLinesStats` 
    WHERE `lineID`='id' 
    ORDER BY `id` DESC) `lastLineStats` 
WHERE `lineID` IN (1873, 1872, 1884, 1883, 1870, 1874, 1876, 1880, 1871, 1877, 1881, 1882, 1885, 1886, 1869, 1875, 1878) 
GROUP BY `lineID`

which is wrong.

Thanks in advance if someone could point me in the right direction for this one.