gpt4 book ai didi

Symfony 学说 native 查询中的未定义索引

转载 作者:行者123 更新时间:2023-12-02 05:20:06 26 4
gpt4 key购买 nike

我正在尝试对我的 FacebookEventResult 实体执行 native 查询,并创建与我的 FacebookEvent 实体的连接。

FacebookEventResult中的相关映射:

/**
* @ORM\Column(type="integer", options={"unsigned":true})
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;

/**
* @ORM\ManyToOne(targetEntity="FacebookEvent", inversedBy="facebookEventResults")
* @ORM\JoinColumn(name="facebook_event_id", referencedColumnName="id", nullable=false)
**/
protected $event;

FacebookEvent中的相关映射:

/**
* @ORM\Column(type="integer", options={"unsigned":true})
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;

/**
* @ORM\OneToMany(targetEntity="FacebookEventResult", mappedBy="event")
**/
protected $facebookEventResults;

我的查询代码:

$rsm = new ResultSetMapping();
$rsm->addEntityResult('AppBundle:FacebookEventResult', 'fer');
$rsm->addFieldResult('fer', 'id', 'id');
$rsm->addFieldResult('fer', 'event_date', 'eventDate');
// $rsm->addFieldResult('fer', 'facebook_event_id', 'event'); // this doesnt help
// some other mappings here as well, also tried without

$rsm->addJoinedEntityResult('AppBundle:FacebookEvent' , 'fe', 'fer', 'event');
$rsm->addFieldResult('fe', 'id', 'id');
// some other mappings here as well, also tried without

$sql = 'SELECT *
FROM facebook_event_result fer
INNER JOIN facebook_event fe ON fer.facebook_event_id = fe.id';

$query = $this->getEntityManager()->createNativeQuery($sql, $rsm);
return $query->getResult();

执行时出现以下错误:

注意:未定义索引:id

我试着关注these instructions来自学说。

最佳答案

参见:https://github.com/doctrine/doctrine2/issues/2482

我在 github 线程中没有看到任何解决方案,但是他们确实提出了一个对我有用的替代方案:ResultSetMapping#addScalarResult($columnName, $alias)

所以,像这样的东西应该可以工作:

$rsm = new ResultSetMapping();
$rsm->addScalarResult('id', 'id');
$rsm->addScalarResult('event_date', 'eventDate');

$sql = 'SELECT *
FROM facebook_event_result fer
INNER JOIN facebook_event fe ON fer.facebook_event_id = fe.id';

$query = $this->getEntityManager()->createNativeQuery($sql, $rsm);
return $query->getResult();

注意:这是对我的临时修复;看起来他们自己的示例不起作用或需要缺少的东西并且没有很好地记录。

关于Symfony 学说 native 查询中的未定义索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31094880/

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