问题
I create a service with methods that run some queries, i want to use doctrine Native SQL but no matter which query i made it always return an empty array. There is something that i'm missing?
Method from service MonthVacancySchedule:
public function getTotalVacanciesByUnits()
{
$rsm = new ResultSetMapping();
$sql = 'SELECT nome_procedimento FROM programacao';
$query = $this->emi->createNativeQuery($sql, $rsm);
$units = $query->getResult();
return $units;
}
The controller which i use the service:
/**
* @Route("/units", name="units")
*/
public function showTotalVacanciesByUnits( MonthVacancySchedule $mvs )
{
$units = $mvs->getTotalVacanciesByUnits();
# Always return empty array.
var_dump($units);
return $units;
}
回答1:
That's because you ask Doctrine for empty array.
Describe fields you need in rsm
object. For example
public function getTotalVacanciesByUnits()
{
$rsm = new ResultSetMapping();
$rsm->addScalarResult('nome_procedimento', 'nome_procedimento');
$sql = 'SELECT nome_procedimento FROM programacao';
$query = $this->emi->createNativeQuery($sql, $rsm);
$units = $query->getResult();
return $units;
}
http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/native-sql.html#scalar-results
来源:https://stackoverflow.com/questions/48829607/doctrine-query-using-native-sql-always-returning-empty-array