All you need to do is to create your custom function in your repository:
public function findItByFoo($var){
$qb = $this->createQueryBuilder('myClass');
$qb->select("myClass")
->where('foo = :var')
->setParameter('var', $var);
$result = $qb->getQuery()->getArrayResult();
return $result;
}
You cannot achieve this with built-in findBy method. More specifically, the findBy definition is:
$repository->findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null);
As you can see for yourself there is not any argument for the hydration mode.
Finally, since you want to hydrate your entities as arrays instead of objects, and you want to do that in your controller, the only solution is something like this:
$data = $this->em->getRepository(Myclass::class)->createQueryBuilder('m')
->select("m")->where('foo = :var')->setParameter('var', $var)
->getQuery()->getArrayResult();
As you can see, it almost identical with the code in the findItByFoo() function I wrote above, I just wanted to mark that you can do that also in your controller (even not suggested, programmatically speaking).
->findBy(['foo' => $var])->toArray( );?undefined functionfindOneBy?findByshould return a doctrine collection, not an individual entity as the error message you posted shows.