gpt4 book ai didi

symfony - Doctrine 2(Symfony)结果集与自定义字段的映射始终为空

转载 作者:行者123 更新时间:2023-12-05 07:47:18 25 4
gpt4 key购买 nike

我正在 Symfony 2 存储库中执行以下 native 查询:

$sql = "SELECT f.id, f.title, 'hi' AS custom_field FROM my_feed f";

$em = $this->getEntityManager();
$rsm = new \Doctrine\ORM\Query\ResultSetMapping($em);
$rsm->addEntityResult('MedicalCoreBundle:Feed', 'f');
$rsm->addFieldResult('f', 'id', 'id');
$rsm->addFieldResult('f', 'title', 'title');
$rsm->addFieldResult('f', 'custom_field', 'custom_field');

$query = $em->createNativeQuery($sql, $rsm);
$result = $query->getResult();

我在 Feed 中创建了非映射属性 custom_field 及其 getter 和 setter getCustomFieldsetCustomField > 实体。

映射字段已正确水化,但当我添加 custom_field RSM 时,出现以下错误:Notice: Undefined index: custom_field [...] in symfony\..\AbstractHydrator。 php.

实体代码

// non mapped field
private $custom_field;

public function getCustomField()
{
return $this->custom_field;
}

public function setCustomField($number)
{
$this->custom_field = (int) $number;

return $this;
}

我在这里做错了什么?

最佳答案

您应该映射 custom_field 或使用 addScalarResult 而不是 addFieldResult

...
$rsm->addScalarResult('custom_field', 'custom_field');
...

结果看起来像

[
[0=>entity, 'custom_field'=>custom_field value],
[0=>entity, 'custom_field'=>custom_field value],
...
]

关于symfony - Doctrine 2(Symfony)结果集与自定义字段的映射始终为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39917872/

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