学说不允许 ResultSetMappingBuilder 工作

2023-08-19php开发问题
2

本文介绍了学说不允许 ResultSetMappingBuilder 工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在使用 Symfony 2 和教义.我已经设置了一个带有自定义查找功能的自定义存储库.当它加入子查询时,我很确定从我读过的内容来看,我需要使用 ResultSetMappingBuilder 从查询中获取结果.但我不断收到以下错误:

I'm using Symfony 2 with doctrine. I've set up a custom repository with a custom find function. As it joins to a subquery I'm pretty sure from what I've read that I'll need to use the ResultSetMappingBuilder to get the results back from the query. But I keep getting the following error:

The column 'id' conflicts with another column in the mapper. 

我的代码是:

    $sql ="SELECT c.id as cid, c.country, c.rank, c.continent, q.*
        FROM country c
        INNER JOIN (
            SELECT d.* , MAX( d.rating ) AS maxrating
            FROM ducks d
            GROUP BY d.country_id
        )q ON ( q.country_id = c.id )";

    $rsm = new DoctrineORMQueryResultSetMappingBuilder($this->getEntityManager());
    $rsm->addRootEntityFromClassMetadata('WfukDuckBundleEntityCountry', 'c', array('id' => 'cid'));
    $rsm->addJoinedEntityFromClassMetadata('WfukDuckBundleEntityDucks', 'ducks', 'c', 'country_id');

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

SQL 查询自行运行,没有任何问题,并返回我期望的结果.

The SQL query runs on it's own without any problems and returns the results I'd expect.

更新,我已经通过重新映射 addRootEntityFromClassMetadata 上的 id 字段修复了 id 问题,但我仍然遇到了修改后的 joinEntity 的遗留问题:

UPDATE, I've fixed the id issue by remapping the id field on the addRootEntityFromClassMetadata, but I've still got a remaining problem with the revised joinedEntity:

    $rsm->addJoinedEntityFromClassMetadata('WfukDuckBundleEntityDucks', 'ducks', 'c', 'country_id');

我收到以下错误:

Notice: Undefined index: country_id in E:DocumentswfukWorkduck travelswampvendordoctrinelibDoctrineORMInternalHydrationObjectHydrator.php line 85 

我很确定这是调用中的最后一个值 country_id,但我不能 100% 确定这是什么.这里的解释:http://readthedocs.org/docs/doctrine-orm/en/latest/reference/native-sql.html 不太清楚,我不完全理解类 docblock 的描述.

I'm pretty sure this is down to the last value in the call, country_id, but I'm not 100% sure what this is. The explanation here: http://readthedocs.org/docs/doctrine-orm/en/latest/reference/native-sql.html isn't too clear and I don't fully understand the description from the class docblock.

它给出的错误是:

The error it gives is:

The column 'id' conflicts with another column in the mapper. 

然而,我已经为其中一个结果重命名了 id 字段,因此结果集只有一列名为id".

Yet I have renamed the id field for one of the results so the result set only has one column called 'id'.

错误的堆栈跟踪显示它被抛出这里:

The stacktrace on the error shows it's being thrown here:

at ResultSetMappingBuilder ->addAllClassFields ('WfukDuckBundleEntityDucks', 'q', array())
in E:DocumentswfukWorkduck travelswampvendordoctrinelibDoctrineORMQueryResultSetMappingBuilder.php at line 71

我怀疑这很明显,但是我可以在任何地方找到有关此功能的文档的方式并不多......

I suspect it's something obvious but there's not much in the way of documentation on this function that I could find anywhere...

已解决:

我无法在 5 小时内将其添加为答案,因此为避免任何人浪费时间回答此问题,答案是:

I can't add this as an answer for another 5 hours, so to avoid anyone wasting time answering this the answer is:

对于任何有同样问题的人来说,问题出在此行上发送的第 4 个参数上:

For anyone who has the same problem the problem was with the 4th parameter sent on this line:

$rsm->addJoinedEntityFromClassMetadata('WfukDuckBundleEntityDucks', 'ducks', 'c', 'ducks');

我用来在其中存储鸭子的 arrayCollection 的 Country 类中应该包含该字段.

Which should have held the field within the Country class that I was using to store the arrayCollection of ducks within it.

推荐答案

刚刚解决了这个问题:

对于任何有同样问题的人来说,问题出在此行上发送的第 4 个参数上:

For anyone who has the same problem the problem was with the 4th parameter sent on this line:

$rsm->addJoinedEntityFromClassMetadata('WfukDuckBundleEntityDucks', 'ducks', 'c', 'ducks');

我用来在其中存储鸭子的 arrayCollection 的 Country 类中应该包含该字段.

Which should have held the field within the Country class that I was using to store the arrayCollection of ducks within it.

这篇关于学说不允许 ResultSetMappingBuilder 工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17