Symfony2 + Doctrine2 不缓存连接实体的结果

发布于 2024-12-22 17:15:46 字数 1487 浏览 1 评论 0原文

我正在使用 Symfony 2.0.10 和 Doctrine 2.1,并且有相当简单的查询(见下文),我想通过 useResultCache(true, 600) 使用 APC(版本 3.1.7,为其启用 1GB 内存)缓存结果) 并将水合模式保持为 \Doctrine\ORM\Query::HYDRATE_OBJECT

问题是多对多关系 (Doctrine\ORM\PersistentCollection) 不会被缓存,并且每次缓存主查询结果时,连接实体都会设置为 null代码>.当我将水合模式设置为 \Doctrine\ORM\Query::HYDRATE_ARRAY 时,相同的查询在 APC 中缓存良好,但这对我来说不是可接受的解决方案,因为我无法为此重做许多模板去工作。

请建议如何在 APC 中缓存所有加入实体的属性?请不要指向文档,因为我想我已经用心记住了它并试图解决这个问题:)

代码:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戏剧牡丹亭 2024-12-29 17:15:46

以下是与缓存问题相关的 Doctrine JIRA 问题,它最接近问题的描述:

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

我的观点是原则 2.2 中固定了这一点

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文