You want to run the following SQL query to grab the data you want:-
SELECT TblB.* FROM tbl_b AS TblB WHERE TblB.id = (
SELECT TblB2.id FROM tbl_b AS TblB2
WHERE TblB2.tbl_a_id = TblB.tbl_a_id
ORDER BY TblB2.id DESC
LIMIT 1
)
ORDER BY TblB.id ASC
See the sqlfiddle for this in action.
To do this in CakePHP you will need to build the sub-query using Cake's buildStatement
:-
$db = $this->TblB->getDataSource();
$subQuery = $db->buildStatement(
array(
'fields' => array('"TblB2"."id"'),
'table' => $db->fullTableName($this->TblB),
'alias' => 'TblB2',
'limit' => 1,
'offset' => null,
'joins' => array(),
'conditions' => array('"TblB"."tbl_a_id" = "TblB2"."tbl_a_id"'),
'order' => '"TblB2"."id" DESC',
'group' => null
)
);
$subQueryExpression = $db->expression($subQuery);
$conditions[] = $subQueryExpression;
$data = $this->TblB->find('all', compact('conditions'));
See the docs for more on retrieving data using a sub-query.