What's the difference in using: getScalarResult() and getArrayResult() when running a query with QueryBuilder in Doctrine:
$query = $this->em->createQueryBuilder();
$query->select(self::SHORT_LIST)
->from(DataSetting::class, 'ds')
->andWhere('ds.'.$field.' LIKE :searchField')
->setParameter('searchField', $value . '%')
->setMaxResults($filters->getLength());
$query->getQuery()->getScalarResult()
and
$query->getQuery()->getArrayResult()
Since this is not well documented on Doctrine I would like to understand the conceptual difference.
Superficially both getArrayResult()
and getScalarResult()
will return similar or same results in your query. Essentially all they change is how the result will be hydrated:
Their corresponding test files show what kind of output they produce, but it might still be a bit unclear.
In simple terms, the ScalarHydrator will return a list of field value mappings with only scalar values. The ArrayHydrator can return a multitude of arrays, a list of associative arrays (key value maps, similar to the ScalarHydrator), but it can also contain objects and the list can be indexed in a certain way and can be nested.
In some cases, especially with simple queries, both hydrators might return the same result. In that case ScalarHydrator likely has less overhead, but whether it actually impacts performance I can't really tell.