Symfony Doctrine Hydrator - With custom hydrator I lose my ManyToOne relation

8k views Asked by At

I would like to extends ObjectHydrator to benefit of the hydration of my ManyToOne relation and add extra field to the Entity.

Here is my hydrator: StatisticsDataHydrator.php

namespace AppBundle\Hydrator\ProjectAssignment;

use AppBundle\Entity\ProjectAssignment;
use Doctrine\ORM\Internal\Hydration\ObjectHydrator;

    class StatisticsDataHydrator extends ObjectHydrator
    {
        /**
         * {@inheritdoc}
         */
        protected function hydrateRowData(array $data, array &$result)
        {
            $hydrated_result = array();
            parent::hydrateRowData($data, $hydrated_result);

            /** @var ProjectAssignment $project_assignment */
            $project_assignment = $hydrated_result[0][0];

            $result[] = $project_assignment;
        }
    }

Here is my config: config.yml

doctrine:
    orm:
        hydrators:
            project_assignment_statisticsdata_hydrator: AppBundle\Hydrator\ProjectAssignment\StatisticsDataHydrator

Where I don't use the hydrator I have no problem:

/**
 * @param ProjectStage $stage
 * @return array
 */
public function findByStageWithStatisticsData(ProjectStage $stage){
    $qb = $this->createQueryBuilder('pa');

    $qb
        //->addSelect('44')
        ->where($qb->expr()->eq('pa.project_stage', ':stage'))
        ->setParameter('stage', $stage);

    return $qb->getQuery()->getResult();
}

enter image description here

But when I use my hydrator:

/**
 * @param ProjectStage $stage
 * @return array
 */
public function findByStageWithStatisticsData(ProjectStage $stage){
    $qb = $this->createQueryBuilder('pa');

    $qb
        ->addSelect('1234') // referencial value
        ->where($qb->expr()->eq('pa.project_stage', ':stage'))
        ->setParameter('stage', $stage);

    return $qb->getQuery()->getResult('project_assignment_statisticsdata_hydrator');
}

enter image description here

The strangest behavior is that the same occure with this config: config.yml

doctrine:
    orm:
        hydrators:
            project_assignment_statisticsdata_hydrator: Doctrine\ORM\Internal\Hydration\ObjectHydrator

I have tried all kind of fetch on relation with no success:

@ORM\ManyToOne(... , fetch="EAGER")
@ORM\ManyToOne(... , fetch="LAZY")
...

Maybe I have to use a Proxy on my Entity, I really don't know :(

Thank you for any help!

2

There are 2 answers

1
Vincent Guyard On

Great! I found the problem, it was with my query builder. I had to manually add the joins and the select of related objects.

/**
 * @param ProjectStage $stage
 * @return array
 */
public function findByStageWithStatisticsData(ProjectStage $stage){
    $qb = $this->createQueryBuilder('pa');

    $qb
        ->addSelect('e') // added
        ->addSelect('r') // added
        ->addSelect('1234')
        ->leftJoin('pa.employee', 'e')  // added
        ->leftJoin('pa.role', 'r')  // added
        ->where($qb->expr()->eq('pa.project_stage', ':stage'))
        ->setParameter('stage', $stage);

    return $qb->getQuery()->getResult('project_assignment_statisticsdata_hydrator');
}

Bonus, here is my Hydrator (it can help someone):

namespace AppBundle\Hydrator\ProjectAssignment;

use AppBundle\Entity\Hydrator\ProjectAssignment\StatisticsData;
use AppBundle\Entity\ProjectAssignment;
use Doctrine\ORM\Internal\Hydration\ObjectHydrator;

class StatisticsDataHydrator extends ObjectHydrator
{
    /**
     * {@inheritdoc}
     */
    protected function hydrateRowData(array $data, array &$result)
    {
        $hydrated_result = array();
        parent::hydrateRowData($data, $hydrated_result);

        /** @var ProjectAssignment $project_assignment */
        $project_assignment = $hydrated_result[0][0];

        $keys = array_keys($hydrated_result); $key = end($keys);

        $statistics_data = new StatisticsData($project_assignment);
        $statistics_data->setTotalWorkedTime((int)$hydrated_result[$key][1]);

        $project_assignment->setStatisticsData($statistics_data);

        $result[] = $project_assignment;
    }
}

In my Entity I have the folowing attribute/getter/setter

/********** NON SYNCED FIELDS **********/

/** @var StatisticsData $statistics_data */
private $statistics_data;

/**
 * @return StatisticsData
 */
public function getStatisticsData()
{
    return $this->statistics_data;
}

/**
 * @param StatisticsData $statistics_data
 */
public function setStatisticsData($statistics_data)
{
    $this->statistics_data = $statistics_data;
}

/***************************************/
0
vctls On

The problem is that the Doctrine SqlWalker will not load meta columns, which include subclasses and associations, if the query Hydration Mode is not HYDRATE_OBJECT or if the query hint HINT_INCLUDE_META_COLUMNS is not set to true:

$addMetaColumns = ! $this->query->getHint(Query::HINT_FORCE_PARTIAL_LOAD) &&
    $this->query->getHydrationMode() == Query::HYDRATE_OBJECT
    ||
    $this->query->getHydrationMode() != Query::HYDRATE_OBJECT &&
    $this->query->getHint(Query::HINT_INCLUDE_META_COLUMNS);

The problem has already been reported in this issue.

As mentioned by the author of the issue, you can either implement the suggested fix, or set the query hint HINT_INCLUDE_META_COLUMNS to true:

$query = $queryBuilder->getQuery();
$query->setHint(Query::HINT_INCLUDE_META_COLUMNS, true);
$result = $query->getResult('CustomHydrator');