gpt4 book ai didi

Symfony Doctrine Hydrator - 使用定制 Hydrator 我失去了 ManyToOne 关系

转载 作者:行者123 更新时间:2023-12-02 13:43:37 31 4
gpt4 key购买 nike

我想扩展 ObjectHydrator 以受益于我的 ManyToOne 关系的 hydration 并向实体添加额外的字段。

这是我的 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;
}
}

这是我的配置: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")
...

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

感谢您的帮助!

最佳答案

太棒了!我发现了问题,问题出在我的查询生成器上。我必须手动添加连接并选择相关对象。

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

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

关于Symfony Doctrine Hydrator - 使用定制 Hydrator 我失去了 ManyToOne 关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46166525/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com