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. (5×5)
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:
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“.