Doctrine2获取每个记录的5个副本 [英] Doctrine2 fetch 5 copy of each record

查看:88
本文介绍了Doctrine2获取每个记录的5个副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常基本的表,几列。它有 15个记录



现在,如果我使用这个查询

  SELECT * FROM excursions WHERE lang_id = 1 
/ pre>

它按照我的预期返回 5 记录



但是,当我使用doctrine2时,会创建一个这样的查询:

  SELECT 
e0_.idx AS idx0,
e0_.place_id AS place_id1,
e0_.lang_id AS lang_id2,
e0_.excursion_name AS excursion_name3,
e0_.excursion_type AS excursion_type4,
e0_.excursion_text_short AS excursion_text_short5,
e0_.start_date AS start_date6,
e0_.end_date AS end_date7,
e0_.override_place AS override_place8,
e0_.languages AS languages9,
e0_.meeting_place AS meeting_place10,
e0_.excursion_text AS excursion_text11,
e0_.starts_at AS starts_at12,
e0_.category_id AS category_id13,
e0_.map_link AS map_link14,
e0_.capacity AS capacity15,
e0_.pick_up_time AS pick_up_time16,
e0_.is_activity AS is_activity17
FROM excursions e1_,excursions e0_
WHERE e0_.lang_id = 1

现在,当我执行这个,它返回 25个记录每个记录的5份副本。它们完全相同,而不是 5 它返回 25 记录。 ( 5x5



哦,是的,如果我删除 excursions e1 _ 它返回5条记录完全。但是为什么?



任何人都可以看这个查询,我是否想念这里?



我使用 QueryBuilder()

  $ qb = $ this-> getRepository() - > createQueryBuilder( '游览'); 
$ qb-> select('e') - > from('Web\Bundle\ExcursionBundle\Entity\Excursions','e');
$ qb-> andWhere(e.langId = 1);

注意:我有其他样式有或没有 QueryBuilder的()。结果总是相同的。 PHP获得 1 各自的副本。我在服务器端执行的查询得到每个记录的 5 副本。



我问这个是因为,如果教义得到多次,如果我把我的整个表格像 500 记录呢?它可能会抓住 500x5 不会减缓进程?

解决方案

没有必要的行。正确的代码是:

  $ qb = $ this-> getRepository() - > createQueryBuilder('e' ; 
$ qb-> andWhere(e.langId = 1);

您的代码中添加了 from 部分两次。当执行 createQueryBuilder 方法(它添加 select 和code>从本身)。您传递从未使用的别名 Excursions 。然后,您从中添加第二个别名为 e


I have a very basic table with few columns. It has 15 records in it.

Now, if i use this query:

SELECT * FROM excursions WHERE lang_id = 1

It returns 5 records as i expected.

But, when i use doctrine2, it creates a query like this:

SELECT
e0_.idx AS idx0,
e0_.place_id AS place_id1,
e0_.lang_id AS lang_id2,
e0_.excursion_name AS excursion_name3,
e0_.excursion_type AS excursion_type4,
e0_.excursion_text_short AS excursion_text_short5,
e0_.start_date AS start_date6,
e0_.end_date AS end_date7,
e0_.override_place AS override_place8,
e0_.languages AS languages9,
e0_.meeting_place AS meeting_place10,
e0_.excursion_text AS excursion_text11,
e0_.starts_at AS starts_at12,
e0_.category_id AS category_id13,
e0_.map_link AS map_link14,
e0_.capacity AS capacity15,
e0_.pick_up_time AS pick_up_time16,
e0_.is_activity AS is_activity17
FROM excursions e1_, excursions e0_
WHERE e0_.lang_id = 1

Now, when i execute this, it returns 25 records! 5 copy for each record. They are exactly same but instead of 5 it returns 25 records. (5x5)

Oh yes, if i remove excursions e1_ it returns 5 records totally. But why?

Can anyone look at that query, did i miss something here?

I used QueryBuilder().

$qb = $this->getRepository()->createQueryBuilder('Excursions');
$qb->select('e')->from('Web\Bundle\ExcursionBundle\Entity\Excursions', 'e');
$qb->andWhere("e.langId = 1");

Note: I did other styles with or without QueryBuilder(). Results are always same. PHP got 1 copy of each. Query that i execute on server side gets 5 copy of each record.

I am asking this because, if doctrine gets multiple times, what if i fetch my whole table like 500 records? It will probably grab 500x5. Wouldn't be slowdown the process?

解决方案

There is not needed line. The correct code would be:

$qb = $this->getRepository()->createQueryBuilder('e');
$qb->andWhere("e.langId = 1");

What you do in your code is adding the "from" part twice. Once it is done by Doctrine when you execute "createQueryBuilder" method (it adds "select" and "from" itself). You pass the alias "Excursions" which is never used. Then you add the second "from" with alias "e".

这篇关于Doctrine2获取每个记录的5个副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆