18

I am using Symfony 2.0.10 with Doctrine 2.1 and have rather simple query (see below), where I want to cache results with APC (version 3.1.7, enabled 1GB of memory for it) via useResultCache(true, 600) and keep hydration mode as \Doctrine\ORM\Query::HYDRATE_OBJECT.

The problem is that Many-to-Many relations (Doctrine\ORM\PersistentCollection) don't get cached and every time when main query results are cached the joined entities are set to null. The same query is cached well in APC when I set hydration mode to \Doctrine\ORM\Query::HYDRATE_ARRAY, but it is not acceptable solution for me, because I can't redo many templates for this to work.

Please suggest how can I cache all joined entities' properties in APC? Please don't point to documentation, because I think I have learned it by heart trying to solve this issue :)

CODE:

$property = $em
->createQueryBuilder()
->select('p,u')
->from('MyBundle:Property', 'p')
->leftJoin('p.users', 'u')
->where('p.id in (:id)')
->setParameter('id', 123)
->getQuery()
->useResultCache(true, 60)
->setHydrationMode(\Doctrine\ORM\Query::HYDRATE_OBJECT)
->getResult();

User.php

class User {
    /**
     * @ORM\ManyToMany(targetEntity="Property", mappedBy="users", cascade={"all"}, fetch="EAGER")
     */
     protected $properties;
}

Property.php

class Property {
    /**
     * @ORM\ManyToMany(targetEntity="User", inversedBy="properties", cascade={"all"}, fetch="EAGER")
     * @ORM\JoinTable(name="user_property",
     *      joinColumns={@ORM\JoinColumn(name="property_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="user_id", referencedColumnName="id")}
     * )
     */
     protected $users;
}
Anton Babenko
  • 6,586
  • 2
  • 36
  • 44
  • 1
    does apc have enough free memory to cache your objects? – meze Dec 25 '11 at 17:36
  • @meze: Yes, enabled 1GB, which is more than enough for this case. APC version is 3.1.7, if matters. – Anton Babenko Dec 25 '11 at 20:28
  • Could you check if this works in 2.2? It has been released today and some fixes to caching mechanisms have been applied. – Ocramius Jan 29 '12 at 23:37
  • @Ocramius: Thanks a lot for your answer! According to changelog for doctrine 2.2 my issue is fixed there, but I should wait when Symfony is released with Doctrine 2.2 support to modify configs properly. – Anton Babenko Jan 30 '12 at 08:34
  • Just swapping submodule references to give it a try could work (obviously not for production... You should reset changes after trying). The DoctrineBundle will will surely need some refactoring... – Ocramius Jan 30 '12 at 12:40
  • Changing just deps files doesn't help, because of changes in AnnotationReader, which throws some errors, but I don't have time this week to work on this project, so will just put this on hold for this week. – Anton Babenko Jan 30 '12 at 18:23
  • What is the configuration of your result cache driver (at EntityManager Configuration level) ? – Florian Klein Feb 11 '12 at 18:18
  • @Florian: I think it is quiet obvious - `result_cache_driver: apc`, or I did understand your question wrong. – Anton Babenko Feb 12 '12 at 15:16
  • 1
    @AntonBabenko no it's obviously the answer I was waiting for :) – Florian Klein Feb 12 '12 at 15:49

1 Answers1

4

Here is the Doctrine JIRA issue related to a caching issue which is the closest to the problem's description:

http://www.doctrine-project.org/jira/browse/DDC-217 https://github.com/doctrine/doctrine2/issues/2861

My opinion is that point is fixed in Doctrine 2.2

ficuscr
  • 6,975
  • 2
  • 32
  • 52
Yves Martin
  • 10,217
  • 2
  • 38
  • 77
  • As @Ocramius said in comments it has been fixed in Doctrine 2.2, but I am waiting for updated DoctrineBundle, so I can verify it in my symfony2 project. Thanks for your answer and time! – Anton Babenko Feb 15 '12 at 14:39
  • Someone started a bounty asking for a reference to an official source... Still waiting for my reps – Yves Martin Feb 23 '12 at 14:55
  • My bounty has expired before you asnwered to the question. I remember it was somebody else, who offered same bounty after me. I accept your answer now. – Anton Babenko Feb 23 '12 at 15:51
  • I have exactly the same problem as in a question, except for I'm using Memcache instead of APC. Using Symfony 2.3 and Doctrine 2.2 – Karmalakas Dec 04 '14 at 11:05
  • Link is broken. Is there any work around? I can't seem to accomplish this in 2.3.3 as Doctrine seems to not use its own find% methods which prevents me simply overriding a repo method to force caching. – ficuscr Sep 15 '16 at 21:09