Usando campos virtuais para somar valores no cakephp
Estou tentando obter a soma de votos para um determinado usuário.
Digamos que esta seja a tabela de posts
id | body | user_id | vote_total | type
1 test 1 4 new
2 test2 1 3 new
3 test3 2 2 new
Estou tentando obter a seguinte saída
user_id | vote_total
1 7
2 2
Aqui está minha função em PostsController
public function topvotes(){
$virtualFields = array('total' => 'SUM(Post.vote_total)');
$total = $this->Post->find('all', array(
array('fields' => array('total'),
'recursive' => 1,
'group' => array('Post.user_id'),
'conditions'=>array('Post.type' => 'new' ))));
$post = $this->Post->find('all', $total);
$this->set('posts', $post);
}
Esta consulta funciona (eu tentei com o phpmyadmin), mas não consigo descobrir como acessar a matriz resultante
editar eu tenho que trabalhar usando a seguinte consulta
$query = $this->Post->query("select posts.user_id, SUM(Posts.vote_total) from posts where posts.type = 'new' group by posts.user_id");
$this->set('posts', $query);
quando eu digito print_r esta é a matriz
Array ( [posts] => Array ( [user_id] => 7 ) [0] => Array ( [total] => 6 ) ) 1