8

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 Answer 1

18

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.