usando campos virtuales para sumar valores en cakephp
Estoy tratando de obtener la suma de votos para un usuario determinado.
Digamos que esto es tabla de mensajes.
id | body | user_id | vote_total | type
1 test 1 4 new
2 test2 1 3 new
3 test3 2 2 new
Estoy tratando de obtener la siguiente salida
user_id | vote_total
1 7
2 2
Aquí está mi función en 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 (lo intenté con phpmyadmin) pero no puedo averiguar cómo acceder a la matriz resultante
Editar Tengo que trabajar con la siguiente 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);
cuando escribo print_r esta es la matriz
Array ( [posts] => Array ( [user_id] => 7 ) [0] => Array ( [total] => 6 ) ) 1