Declaring a virtual field for this may be tricky, so I suggest you to add it on the fly whenever you need it.
So your HousesController
's index action will look like this:
public function index() {
$this->House->virtualFields['last_visit'] = 'MAX(Visit.date)';
$this->paginate = array(
'House' => array(
'group' => array('house_id'),
'joins' => array(
array(
'table' => 'visits',
'alias' => 'Visit',
'type' => 'INNER',
'conditions' => array(
'House.id = Visit.house_id',
)
)
)
)
);
$this->set('houses', $this->paginate());
}
Notice you have to remove the $publicFields
declaration from your model, and that I changed the name of the field to from lastvisit
to last_visit
to make it more Cake.
Hope it helps.