I'm doing a small project in Symfony 2.8
, and what I'm trying to do is perform a query doctrine
that adds the values of a column of a table and sends the total to my página.html.twig
.
Here is the code I have written in the controller class where I want to perform this operation:
class DefaultController extends Controller
{
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$query_ganancia = $em->createQuery('
SELECT SUM(v.ingresos)
FROM CarroBundle:Viaje v
');
$var_ganancia = $query_ganancia->getResult();
return $this->render('CarroBundle:Default:index.html.twig', array('ganancia'=> $var_ganancia));
}
}
And in the page I get the value as follows:
<h1>{{ ganancia }}</h1>
But for some reason that I still don't see, it keeps returning this exception :
An exception has been thrown during the rendering of a template ("Notice: Array to string conversion") in CarroBundle:Default:index.html.twig at line 39.
I have searched the Symfony 2.8 guide regarding Doctrine
and tried to perform the operation in this other way without result:
$var_ganancia = $query_ganancia->setMaxResults(1)->getOneOrNullResult();
I would like someone to guide me on this error. Thanks.
You have to perform an array treatment on the gain variable in Twig: