Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 + Doctrine2 is not caching results of joined entities

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;
}
like image 679
Anton Babenko Avatar asked Dec 25 '11 12:12

Anton Babenko


1 Answers

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

like image 121
Yves Martin Avatar answered Sep 24 '22 05:09

Yves Martin