0
votes

I have a site that is for a video game I play and am working on improving the performance of the site by implementing some additional caching. I've already been able to implement query result caching on custom repository functions, but haven't been able to find anywhere that explains how I can include query result caching on the built in functions (findOneById, etc). I'm interested in doing this because many of my database queries are executed from these 'native' repository functions.

So as an example I have a character entity object with the following properties: id, name, race, class, etc.

Race and class in this object are references to other entity objects for race and class. When I load a character for display I get the character by name (findOneByName) and then in my template I display the character's race/class by $characterObject->getRace()->getName(). These method calls in the template result in a query being run on my Race/Class entity tables fetching the entity by id (findOneById I assume).

I've attempted to create my own findOneById function in the repository, but it is not called under these circumstances.

How can I setup doctrine/symfony such that these query results are cache-able?

I am running Symfony 2.1.3 and doctrine 2.3.x

2

2 Answers

1
votes

I've found out that it isn't possible to enable query cache on doctrine build in functions. I will post a link which explains why later after I find it again.

0
votes

Your entities probably look something like this:

MyBundle\Entity\Character:
    type: entity
    table: Character
    fields:
      id:
        id: true
        type: bigint
      name:
        type: string
        length: 255
    manyToOne:
      race:
        targetEntity: Race
        joinColumns:
          raceId:
            referencedColumnName: id

MyBundle\Entity\Race:
    type: entity
    table: Race
    fields:
      id:
        id: true
        type: bigint
      name:
        type: string
        length: 255
    oneToMany:
      characters:
        targetEntity: Character
        mappedBy: race

If that's the case, then modify your Character entity mapping so that it eagerly loads the Race entity as well:

MyBundle\Entity\Character:
  ...
    manyToOne:
      race:
        targetEntity: Race
        joinColumns:
          raceId:
            referencedColumnName: id
        fetch: EAGER

Doctrine documentation on the fetch option: @ManyToOne