从表中选择记录,其中相关表的最后一个在Yii2中等于x

How can I build query in search model to select values from table_a where related table_b last inserted record is equal to something.

My tables looks something like that

table_a

  • id
  • name
  • user_id

table_b

  • id
  • table_a_id
  • status
  • some_other_status
  • timestamp

At this moment my query in search model looks like that:

TableA::find()->joinWith(['user', 'TableB'])->groupBy(['table_a.id', 'table_b.table_a_id']);

Query generated by ActiveRecord have to give same results like query

SELECT a.* FROM table_a OUTER LEFT JOIN (SELECT status, some_other_status FROM table_b ORDER BY timestamp LIMIT 1) as b ON b.table_a_id = a.id WHERE b.status = X;