0
votes

I am currently refactoring an application written in Symfony 3 and relies heavily on Doctrine ORM and I'm stuck trying to get an object/array with indexes of my selected columns.

Now I am fairly familiar with PHP PDO and as I recall a basic fetch of query results as shown below

$sth->fetchAll();

(Depending on my query) it would give me an array similar to the one below

  [0] => Array
    (
        [name] => pear
        [0] => pear
        [colour] => green
        [1] => green
    )

[1] => Array
    (
        [name] => watermelon
        [0] => watermelon
        [colour] => pink
        [1] => pink
    )

With Doctrine i have tried to use several inbuilt functions with Hydration parameters

$query->getResult(); 

And with no luck I end up getting something like this

Array
(
  [0] => Array
    (
        [name] => pear
        [colour] => green
    )

 [1] => Array
    (
        [name] => Watermelon
        [colour] => Pink
    )
)

Can someone help me or point me in the right direction how to properly have this sorted out?

------Updated the question to include the full method I am currently using----

public function getDepartmentCount()
{
    $qb = $this->createQueryBuilder('fruit')
        ->leftJoin('fruit.color','color')
    $query=$qb->getQuery();
    return $query->getArrayResult(); //Method that possibly needs to be changed
}
1

1 Answers

1
votes

I have been able to solve this on my own after creating a custom Hydrator.

I will keep the solution here for anyone who might face a similar problem.

Below is a class for the custom Hydrator

namespace AppBundle\Doctrine;
use Doctrine\ORM\Internal\Hydration\AbstractHydrator;
use PDO;

class CustomHydrator extends AbstractHydrator
{
    /**
    * Hydrates all rows from the current statement instance at once.
    *
    * @return array
    */
    protected function hydrateAllData()
    {
        // TODO: Implement hydrateAllData() method.
        return $this->_stmt->fetchAll(PDO::FETCH_NUM);
    }
}

Added this in my config file under the orm section to tell Symfony where to find the Custom Hydrator and its name

hydrators:
          GridDataHydrator: AppBundle\Doctrine\CustomHydrator

And Finally executed the query using this method

$query->getResult('GridDataHydrator');