我开发了一种“统计 - 网络”。
例如,我有一些博客条目,每个访问者都有一个额外的统计条目。
示例博客实体:
/** * @ORM\ManyToMany(targetEntity="Statistic", inversedBy="blogid") * @ORM\JoinTable(name="blog_statistics") */ private $statistics;示例统计实体:
/** * @ORM\ManyToMany(targetEntity="Blog", mappedBy="statistics") */ private $blog;在统计实体中,我有更多的字段,如“时间,用户,IP”。 在博客实体中,我有“文本,标题,时间”等字段。
一开始我有1个入口。 一切都很好/好。
一周后,我有2个博客条目的5.000个条目(DB)。 (每篇博客2.500)我得到了php内存问题。
我认为doctrine试图将所有2.500个条目加载到RAM / Cache中。 但我只需要最后一个用于“最后访问”的信息。 如果我需要,我可以获得其余的条目。 (统计概览)
什么是最好的“限制”条目? 当前通话:“Repository-> fetchAll”
i develope a type of "Statistics-Web".
For example i have some blog entries and for each visitor a extra statistic entry.
Example blog entity:
/** * @ORM\ManyToMany(targetEntity="Statistic", inversedBy="blogid") * @ORM\JoinTable(name="blog_statistics") */ private $statistics;Example statistic entity:
/** * @ORM\ManyToMany(targetEntity="Blog", mappedBy="statistics") */ private $blog;In the entity for statistic i have more fields like "time, user, ip". In the Blog entity i have fields like "text, title, time".
At the beginning i had 1 Entry. Everything is working / good.
A week later, i had 5.000 Entries (DB) for 2 Blog Entries. (2.500 per blog entry) I get php memory problems.
I think doctrine tries to load all 2.500 Entries into the RAM/Cache. But i only need the last one for the "last visited" information. I could get the rest of the entries if i needed them. (statistics overview)
Whats the best to "limit" entries? Current call: "Repository->fetchAll"
最满意答案
解决方案非常明显:
$lastStatisticsRecord = $repository->createQueryBuilder('s') ->orderBy('s.time', 'DESC') ->setMaxResults(1) ->getQuery() ->execute();此查询将仅从表中选择最后一个统计实体。 如果您需要获取包含最后一个统计条目的博客条目,只需创建一个JOIN语句:
$lastStatisticsRecord = $repository->createQueryBuilder('s') ->select(array('s', 'b')) ->leftJoin('s.blogid', 'b') ->orderBy('s.time', 'DESC') ->setMaxResults(1) ->getQuery() ->execute();Problem solved...
Only use "fetch" option in many-to-many relation:
@ORM\ManyToMany(targetEntity="Statistic", inversedBy="blogid", fetch="EXTRA_LAZY")Then you could use this function to get latest statistic entry:
public function getLatestStatistic() { $cur = array(); if(count($this->getStatistics()) > 0) { $cur = $this->getStatistics()->slice(count($this->getStatistics()) - 1, 1); } return count($cur) > 0 ? $cur[0] : null; }更多推荐
发布评论