Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5980351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T21:47:18+00:00 2026-05-22T21:47:18+00:00

I’m working with Doctrine 1.2 in Symfony 1.4 on PHP5.3. I have a database,

  • 0

I’m working with Doctrine 1.2 in Symfony 1.4 on PHP5.3.

I have a database, in which events have multiple start datetimes (1:n) and types (n:m).

For a program, I want a list of start times and event title, which belong to a special type and are not outdated:

$dql = Doctrine_Query::create()
  ->select('ed.start_datetime,e.title')/*edd.*,*/
  ->from('EventDate ed')
  ->leftJoin('ed.Event e')
  ->leftJoin('e.EventTypes et')
  ->leftJoin('e.Domain d')
  ->orderBy('ed.start_datetime, ed.event_id');
  ->where('e.start_datetime >= ? AND e3.id = ?',array('2011-05-27 17:19:41',2))
  ->fetchArray();

The schema can be reviewed at pastebin.com/SdjvsaxW.

Now, doctrine makes two statements

SELECT DISTINCT e5.event_id FROM event_date e5 LEFT JOIN event e6 ON e5.event_id = e6.id LEFT JOIN event_event_type e8 ON (e6.id = e8.event_id) LEFT JOIN event_type e7 ON e7.id = e8.event_type_id WHERE (e5.start_datetime >= '2011-05-27 17:19:41' AND e7.id = '2') ORDER BY e5.start_datetime, e5.event_id

SELECT e.event_id AS e__event_id, e.start_datetime AS e__start_datetime, e2.id AS e2__id, e2.title AS e2__title FROM event_date e LEFT JOIN event e2 ON e.event_id = e2.id LEFT JOIN event_event_type e4 ON (e2.id = e4.event_id) LEFT JOIN event_type e3 ON e3.id = e4.event_type_id WHERE e.event_id IN (*all the IDs from the first query*) AND (e.start_datetime >= '2011-05-27 17:19:41' AND e3.id = '2') ORDER BY e.start_datetime, e.event_id

The problem starts when I introduce pagination: When I add an offset, it will be added in the first query. Now, as there might be more than one start time for an event, the number of IDs from the first SELECT DISTINCT query may be smaller than the number of dates. Consequently, dates may be skipped. In my case, there are even less than 50 different events, so the second pages displaying event date 51-100 is indeed empty, despite the fact that there are several hundred event dates (due to recurring events).

I think the problem might be solved leaving away the distinct, but I cannot control that.

Anyone any idea?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T21:47:19+00:00Added an answer on May 22, 2026 at 9:47 pm

    Doctrine runs two queries, the first a distinct, in a mode called “limit subquery”, when you have a base table and join across a one-to-many relationship. This is because you are going to select the product of the join, and will end up with more rows than there are records in the base table. For example, the database will return Event #5 in two rows if there are two EventTypes for Event #5. This two query behavior is in MySQL only, other databases can run a limit in a subquery, and Doctrine generates a single query.

    Since your Events have multiple dates, and EventDate has a compound primary key, Doctrine SHOULD select a distinct set of (event_id, start_datetime) in the first query, but it seems to be ignoring the second part of your compound primary key, probably because it can’t stuff them into the ON clause in the second query.

    You can probably work around this in one of two, or maybe three, ways:
    1) add a unique primary key to EventDate (autoincrement column). you likely don’t have to reference it anywhere else, but doctrine will start using that
    2) do the 2 queries manually. Select all of the distinct event_id, start_datetime from EventDate, add your limit and offset, and then programmatically create an where clause:

     ... WHERE ((event_id = ? AND start_datetime = ?) OR (event_id = ? AND start_datetime = ?))... 
    

    3) Reorder your query to start from a different table or add a DISTINCT in the select. Try selecting distinct(ed.event_id, ed.start_datetime). Sometimes these things can avoid the limit-subquery algorithm from firing.
    4) Turn this system off, see link below, never tried this and I’m not sure what side effects there are in hydration and the doctrine object cache.

    Personally, I would go with #1. I’ve found that it always helps to have that extra primary key column with Doctrine, even when you can form a primary key across multiple columns.

    If you want to see the code doctrine uses for this, see lib/Doctrine/Record.php, function getLimitSubquery(). You’ll note the comment “// what about composite keys?” in the code twice. You can also read http://www.doctrine-project.org/documentation/manual/1_2/en/dql-doctrine-query-language:limit-and-offset-clauses:the-limit-subquery-algorithm

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a text area in my form which accepts all possible characters from
I have a reasonable size flat file database of text documents mostly saved in
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.