php - Yii2: subquery within model relation -


from 3 models/tables:

-orders (id | name | comments | ...) -orderslines (id | orderid | name | sdate | edate | comments | ...) -orderslinesstats (id | lineid | date | status | ...) 

i need last entered record orderslinesstats given array of lineids (the ones in hasmany relation) can done mysql subquery:

select *      (select * `orderslinesstats`          `lineid` in (1873, 1872, 1884, 1883, 1870, 1874, 1876, 1880, 1871, 1877, 1881, 1882, 1885, 1886, 1869, 1875, 1878)         order `orderslinesstats`.`id` desc     )     laststats group `lineid` 

so, in orderslines model 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 have:

public function getorderslines()     {         return $this             ->hasmany(orderslines::classname(), ['orderid' => 'id'])             ->orderby(['typeid' => sort_asc, 'name' => sort_asc])             ->with(['lastlinestats']);     } 

but resulting query when getting $model->orderslines this:

select *  (select * `orderslinesstats`      `lineid`='id'      order `id` desc) `lastlinestats`  `lineid` in (1873, 1872, 1884, 1883, 1870, 1874, 1876, 1880, 1871, 1877, 1881, 1882, 1885, 1886, 1869, 1875, 1878)  group `lineid` 

which wrong.

thanks in advance if point me in right direction one.


Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -