Doctrine query using Native SQL always returning empty array

牧云@^-^@ 提交于 2021-02-20 13:32:13

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!