ActiveRecord relation with a 'group' parameter issue

Hi all,

I'm not sure if it's a bug or not but, here is a strange ActiveRecord behavior I found :

When I setup a HAS_MANY relation with a 'group' parameter and try to do a eager loading using a 'with' method (ie MyModel::model()->with($join)->findAll()), the resulting queries are done accordingly but trying to access the related object in my view also produce a lazy loading for each related relational objects.

This is not happening if I remove the 'group' parameter.

Can anybody confirm that and tell me if it is a bug or something I do wrong?

Could you give more details? What is $join and the relation definition, and what are the generated SQLs?

Quote

Could you give more details? What is $join and the relation definition, and what are the generated SQLs?

OK.

So, the relations definition is as follow:

In the controller, I use :

In the related view, I use something like (I tried many different things) :

Extract from the trace Log result:

  • Excursion.findAll() eagerly

  • query with SQL: SELECT rdn_excursions.id AS t0_c0, rdn_excursions.created AS t0_c1, rdn_excursions.modified AS t0_c2, rdn_excursions.user_id AS t0_c3, rdn_excursions.published AS t0_c4, rdn_excursions.title AS t0_c5, rdn_excursions.excursion_level_id AS t0_c6, rdn_excursions.route AS t0_c7, rdn_excursions.max_altitude AS t0_c8, rdn_excursions.min_altitude AS t0_c9, rdn_excursions.difference_in_altitude AS t0_c10, rdn_excursions.excursion_duration_id AS t0_c11, rdn_excursions.beaconing AS t0_c12, rdn_excursions.lat AS t0_c13, rdn_excursions.lng AS t0_c14, rdn_excursions.comments AS t0_c15, user.id AS t1_c0, user.login AS t1_c4, level.id AS t2_c0, level.name AS t2_c1, duration.id AS t3_c0, duration.name AS t3_c1 FROM rdn_excursions LEFT OUTER JOIN rdn_users user ON rdn_excursions.user_id=user.id LEFT OUTER JOIN rdn_excursion_levels level ON rdn_excursions.excursion_level_id=level.id LEFT OUTER JOIN rdn_excursion_durations duration ON rdn_excursions.excursion_duration_id=duration.id WHERE published=1

  • query with SQL: SELECT rdn_excursions.id AS t0_c0, shortlocations.id AS t4_c0, shortlocations.excursion_id AS t4_c1, shortlocations.country_name AS t4_c2, shortlocations.country_name_code AS t4_c3, shortlocations.administrative_area_name AS t4_c4, shortlocations.sub_administrative_area_name AS t4_c5, shortlocations.locality_name AS t4_c6, shortlocations.postal_code_number AS t4_c7, shortlocations.thoroughfare_name AS t4_c8 FROM rdn_excursions LEFT OUTER JOIN rdn_excursions_locations shortlocations ON shortlocations.excursion_id=rdn_excursions.id WHERE rdn_excursions.id IN (12, 11, 10, 9, 8, 5, 3, 1) GROUP BY shortlocations.country_name, shortlocations.administrative_area_name, shortlocations.sub_administrative_area_name ORDER BY shortlocations.country_name, shortlocations.administrative_area_name, shortlocations.sub_administrative_area_name

So far, so good, then suddenly ;)

  • lazy loading Excursion.shortlocations

  • query with SQL: SELECT rdn_excursions.id AS t0_c0, shortlocations.id AS t1_c0, shortlocations.excursion_id AS t1_c1, shortlocations.country_name AS t1_c2, shortlocations.country_name_code AS t1_c3, shortlocations.administrative_area_name AS t1_c4, shortlocations.sub_administrative_area_name AS t1_c5, shortlocations.locality_name AS t1_c6, shortlocations.postal_code_number AS t1_c7, shortlocations.thoroughfare_name AS t1_c8 FROM rdn_excursions LEFT OUTER JOIN rdn_excursions_locations shortlocations ON shortlocations.excursion_id=rdn_excursions.id WHERE rdn_excursions.id=12 GROUP BY shortlocations.country_name, shortlocations.administrative_area_name, shortlocations.sub_administrative_area_name, shortlocations.country_name, shortlocations.administrative_area_name, shortlocations.sub_administrative_area_name ORDER BY shortlocations.country_name, shortlocations.administrative_area_name, shortlocations.sub_administrative_area_name

And so on for each resultset rows.

Removing the 'group' property from the relations definition make the lazy loading go away.

I use the 1.0.4 final release (tried the 1.0.5 with no differences).

Do you need more informations?

EDIT: I didn't noticed it at first, but if you look at the lazy loading query, you will see that the GROUP BY statement is doubled… It may be related to the strange behaviour?

Could you please create a ticket about this? I need some time to investigate this issue. Thanks.

Quote

Could you please create a ticket about this? I need some time to investigate this issue. Thanks.

Done. Thank you for your help.

This issue has been fixed (update to the last SVN release).

See http://code.google.c…s/detail?id=258

Thank you Qiang  :)