Symfony2 + Doctrine2 is not caching results of joined entities

本秂侑毒 提交于 2019-11-29 00:57:03

问题


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;
}

回答1:


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



来源:https://stackoverflow.com/questions/8629640/symfony2-doctrine2-is-not-caching-results-of-joined-entities

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!