使用 Doctrine (Symfony2) 查询表时缺少行

Missing rows when querying table with Doctrine (Symfony2)(使用 Doctrine (Symfony2) 查询表时缺少行)
本文介绍了使用 Doctrine (Symfony2) 查询表时缺少行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I'm encountering a strange issue with Doctrine.

I need to query a simple table with only 1 inner join, which I something I have already done many times. But in this case something's odd: there are a lot of rows missing.

I have an entity called Policy. It is linked to a table on my Oracle database. There are 81k+ rows in this table. When querying this entity with the Doctrine query builder, I only get 5k results. I made this query as simple as possible for testing :

$qb = $em->createQueryBuilder();
$qb->select('p')->from('ErwMonitoringExtranetBundle:Policy', 'p');
$query = $qb->getQuery();
$policiesFull = $query->getResult();

The $policiesFull variable only contains 5k elements. There are no duplicates in the table.

The SQL query that is generated by Doctrine looks like this :

SELECT
  r0_.node_group_name      AS NODE_GROUP_NAME0,
  r0_.policy_name          AS POLICY_NAME1,
  r0_.policy_description   AS POLICY_DESCRIPTION2,
  r0_.policy_group_name    AS POLICY_GROUP_NAME3,
  r0_.policy_type_name     AS POLICY_TYPE_NAME4,
  r0_.policy_name_on_agent AS POLICY_NAME_ON_AGENT5,
  r0_.date_last_maj        AS DATE_LAST_MAJ6,
  r0_.om_name              AS OM_NAME7,
  r0_.id_node              AS ID_NODE8
FROM
  ewintranet.ref_monitored_ci;

Running the same exact query on Oracle returns the full table content.

Counting results through a doctrine query returns the correct number of rows :

$qb = $em->createQueryBuilder();
$qb->select('count(p)')->from('ErwMonitoringExtranetBundle:Policy', 'p');
$query = $qb->getQuery();
echo $query->getSingleScalarResult();

This returns 81k.

Does anybody know why all these rows disappear after using getResult() ?

解决方案

Okay, I found out what was causing my issue. The primary ID was wrong in my Entity declaration.

The oracle table had a composed primary key while in my entity the ID was only on one column. GetResult was making a DISTINCT on this column.

这篇关于使用 Doctrine (Symfony2) 查询表时缺少行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)