-1

I am using the Doctrine findBy() method, but I want to get arrays returned instead of objects. This is my query:

$results = $em
    ->getRepository('xBundle:x')
    ->findBy(
        array(),
        array('name' => 'ASC')
);

I did some research and found that HYDRATE_ARRAY is the way to do it, but I have no idea how to include that in the findBy() method.

EDIT My query as it is, is returning objects. I don't want to create any methods in my repository; I want to just edit the existing query so that it returns arrays.

Jolan
  • 681
  • 12
  • 39
  • 1
    Possible duplicate of [How to return an array not an object with doctrine findOneBy method in symfony2?](https://stackoverflow.com/questions/23681567/how-to-return-an-array-not-an-object-with-doctrine-findoneby-method-in-symfony2) – Denis Alimov Apr 02 '18 at 07:22
  • 1
    you can not do it without creating method in repository – Denis Alimov Apr 02 '18 at 07:46
  • 1
    https://stackoverflow.com/questions/42910599/how-to-get-array-results-in-findall-doctrine – Denis Alimov Apr 02 '18 at 07:46
  • 1
    Why the explicit need for arrays instead of objects? Anyway, you can just cast objects to arrays (`$array = (array) $object;`) in php: [Link](https://3v4l.org/0MMXM). See the "Converting to Array" paragraph in the official [docs](http://www.php.net/manual/en/language.types.array.php) for notable exceptions: integer properties are unaccessible; private variables have the class name prepended to the variable name; protected variables have a '*' prepended to the variable name. These prepended values have null bytes on either side. This can result in some unexpected behaviour:* – ccKep Apr 02 '18 at 11:34

1 Answers1

2

craete a function into repository and then create a query builder

/**
 * @return array
 */
public function getArray()
{
    return $this
        ->createQueryBuilder('x')
        ->orderBy('x.name', 'DESC')
        ->getQuery()
        ->getArrayResult();
}

access to your function from controller

$results = $this->getDoctrine()->getRepository('xBundle:x')->getArray();

More info here

Shay Altman
  • 2,720
  • 1
  • 16
  • 20