Symfony Doctrine Hydrator - 使用定制保湿器,我失去了我的ManyToOne关系

时间:2017-09-12 01:28:02

标签: symfony doctrine-orm doctrine many-to-one hydration

我想扩展ObjectHydrator以利用我的ManyToOne关系的水合作用,并为实体添加额外的字段。

这是我的保湿器: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;
        }
    }

这是我的配置:config.yml

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

如果我不使用保湿剂,我没有问题:

/**
 * @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

但是当我使用我的保湿器时:

/**
 * @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

最奇怪的行为是这个配置发生同样的事情:config.yml

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

我尝试过对关系进行所有类型的获取但没有成功:

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

也许我必须在我的实体上使用代理,我真的不知道:(

感谢您的帮助!

2 个答案:

答案 0 :(得分:2)

大!我找到了问题,这是我的查询构建器。我不得不手动添加连接和相关对象的选择。

/**
 * @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');
}

奖金,这是我的保湿器(它可以帮助某人):

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;
    }
}

在我的实体中,我有以下属性/ 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;
}

/***************************************/

答案 1 :(得分:0)

问题在于,如果查询水化模式不是HYDRATE_OBJECT或查询提示HINT_INCLUDE_META_COLUMNS为true,则Doctrine SqlWalker将不会加载包括子类和关联的元列:

$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);

该问题已在this issue中报告。

如问题作者所提到的,您可以实施建议的修复程序,也可以将查询提示HINT_INCLUDE_META_COLUMNS设置为true:

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