简体   繁体   中英

Doctrine triggering too many database queries

I've been working on a problem in Symfony for a few weeks and getting nowhere.

Event and Playlist have a one to many relationship. The relevant part of my schema.yml file:

Event:
  actAs:
    Timestampable: ~
  columns:
    date:
      type: date(25)
      default: '0000-00-00'
      notnull: true
    name:
      type: string(60)
      notnull: true
    host_title_id:
      type: integer
    broadcast_format:
      type: enum(7)
      values:
        - Radio
        - Live
        - Podcast
        - Video
      default: Podcast
      notnull: true
    long_description:
      type: string()
      notnull: true
    geography_id:
      type: integer
      default: NULL
    has_been_emailed:
      type: boolean
      default: '0'
      notnull: true
    active:
      type: boolean
      default: '0'
      notnull: true

Playlist:
  actAs:
    Timestampable: ~
  columns:
    show_time: string(40)
    recorded:
      type: boolean
      default: '0'
      notnull: true
    title: string(100)
    event_id: integer
    podcast_id: integer
  relations:
    Event:
      local: event_id
      foreign: id
      foreignAlias: Playlists

The following code is okay, and causes one query to the database:

return Doctrine_Query::create()
            ->from('Playlist P')
            ->leftJoin('P.Event E')
            ->limit(1)->execute();

The following code is NOT okay, and causes SIX queries to the database:

return Doctrine_Query::create()
            ->from('Event E')
            ->leftJoin('E.Playlists P')
            ->limit(1)->execute();

Worse, with this second statement, the object that comes back has no data in the [Playlists] part. Ie it looks like this:

sfOutputEscaperArrayDecorator Object
(
    [count:sfOutputEscaperArrayDecorator:private] => 1
    [value:protected] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [date] => 1998-08-01
                    [name] => Showname
                    [host_title_id] => 1
                    [broadcast_format] => Radio
                    [long_description] => This is an episode.
                    [geography_id] => 25
                    [has_been_emailed] => 1
                    [active] => 1
                    [created_at] => 0000-00-00 00:00:00
                    [updated_at] => 0000-00-00 00:00:00
                    [Playlists] => Array
                        (
                        )

                )

        )

    [escapingMethod:protected] => esc_specialchars
)

What is the difference between these two orders? I don't understand.

UPDATE: if I change the relation named Playlists to TestPlaylists in the schema and DQL, it works much better. It still fires one query more than I'd expect, but the object that comes back is populated with the correct data.

What Symfony version are you using? I've just tested your schema in a Symfony 1.4.18 project and when I take out the limit from the Doctrine query both of them only create one query to the DB.

If I keep the limit in there the second Doctrine query created one extra DB query (ie select distinct on Event).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM