Ticket #1380 (closed defect: fixed)

Opened 19 months ago

Last modified 18 months ago

Lack of a clear way to access calculated DQL columns

Reported by: francois Owned by: jwage
Priority: minor Milestone: 1.0.3
Component: Attributes Version: 1.0.0
Severity: Keywords:
Cc: Has Test: no
Status: Pending Core Response Has Patch: no

Description

When adding calculated DQL columns, Doctrine tries to find a way to make it available from the base record, but can't figure out what to do when the calculation is on several objects.

Here is a DQL example:

//Article has many Comments
SELECT a.*, COUNT(a.Comment) NbComment FROM Article a GROUP BY a.id LIMIT 1
// will hydrate an Article object looking like
-
  id: 456
  title: Hello, world
  body: Lorem Ipsum...
  Comment:
    id: ~
    body: ~
    article_id: ~
    NbComment: 23

So that works, since I can retrieve my custom NbComment column by calling $article['Comment']['NbComment'].

But when the calculation is on more than one class, the hydrator has no way to determine where to attach the new column.

Since the current behavior is not very intuitive and very limited, I suggest that calculated columns should be made available directly to the base record - providing they have an alias, which should be compulsory. That way, using the previous DQL statement, the alias NbComments could be retrieved by calling $article['NbComment'].

Attachments

1380.diff Download (1.7 KB) - added by jwage 18 months ago.

Change History

Changed 19 months ago by jwage

  • milestone changed from Unknown to 1.0.0-RC2

Changed 19 months ago by jwage

  • version changed from 0.11 to 1.0
  • milestone changed from New to 1.0.1

Changed 18 months ago by jwage

Added coverage in r4897 which tests that calculated columns are hydrate in to the component/dql alias that is first encountered in the expression. The 2nd test checks that the value is also hydrated in to root component as well.

Changed 18 months ago by jwage

This patch fixes the problem and hydrates the values in to the root component as well. We'll review it and hopefully get something committed for 1.0.1 to fix the problem.

Changed 18 months ago by jwage

Changed 18 months ago by jwage

After thinking about this, the behavior is consistent and correct. The calculate value is always hydrated in to the first component involved in the expression.

SELECT u.*, p.*, (COUNT(p.id) + COUNT(u.id)) AS test_calculated_column FROM User u, u.Phonenumber p

In the above example the test_calculated_column would be hydrated in to:

$users[0]['Phonenumber'][0]['test_calculated_column']
SELECT u.*, p.*, (COUNT(u.id) + COUNT(p.id)) AS test_calculated_column FROM User u, u.Phonenumber p

This would be hydrated in to the root component since u.id it the first component encountered in the query:

$users[0]['test_calculated_column']

So, after seeing how it really works. It seems that nothing is wrong, we just need to document that calculated column values are always hydrated in to the first component involved in the expression.

Changed 18 months ago by jwage

  • status changed from new to closed
  • resolution set to invalid

Changed 18 months ago by francois

  • status changed from closed to reopened
  • resolution invalid deleted

To make things clear, we need a programmatic way to retrieve calculated columns.

Changed 18 months ago by jwage

  • milestone changed from 1.0.1 to 1.0.3

Changed 18 months ago by jwage

  • status changed from reopened to closed
  • resolution set to fixed

In r4993 this was fixed. Thanks for the report.

Note: See TracTickets for help on using tickets.