Permalink
Browse files

Add Post Count caching, displays on forum posts.

  • Loading branch information...
reecefowell committed Mar 7, 2012
1 parent 55caf40 commit e49ceb9cd3f03b049f874e842ce2080957f11197
@@ -39,6 +39,17 @@ class PostManager extends BaseManager implements EntityManagerInterface
protected $postCount;
+ public function flushNow()
+ {
+ parent::flushNow();
+
+ $user = $this->container->get('security.context')->getToken()->getUser();
+
+ $this->container->get('ccdn_forum_forum.registry.manager')->updateCachePostCountForUser($user);
+ }
+
+
+
/**
*
* @access public
@@ -69,6 +80,7 @@ public function insert($post)
$this->container->get('ccdn_forum_forum.board.manager')->updateBoardStats($topic->getBoard())->flushNow();
+
return $this;
}
@@ -0,0 +1,54 @@
+<?php
+
+/*
+ * This file is part of the CCDNForum ForumBundle
+ *
+ * (c) CCDN (c) CodeConsortium <http://www.codeconsortium.com/>
+ *
+ * Available on github <http://www.github.com/codeconsortium/>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace CCDNForum\ForumBundle\Entity\Manager;
+
+use CCDNComponent\CommonBundle\Entity\Manager\EntityManagerInterface;
+use CCDNComponent\CommonBundle\Entity\Manager\BaseManager;
+
+use CCDNForum\ForumBundle\Entity\Registry;
+
+/**
+ *
+ * @author Reece Fowell <reece@codeconsortium.com>
+ * @version 1.0
+ */
+class RegistryManager extends BaseManager implements EntityManagerInterface
+{
+
+
+
+ /**
+ *
+ * @access public
+ * @param $user
+ */
+ public function updateCachePostCountForUser($user)
+ {
+
+ $record = $this->container->get('ccdn_forum_forum.registry.repository')->findRegistryRecordForUser($user->getId());
+
+ if ( ! $record)
+ {
+ $record = new Registry();
+ $record->setOwnedBy($user);
+ }
+
+ $postCount = $this->container->get('ccdn_forum_forum.post.repository')->getPostCountForUserById($user->getId());
+
+ $record->setCachePostCount($postCount['postCount']);
+
+ $this->persist($record)->flushNow();
+ }
+
+}
@@ -32,6 +32,17 @@ class TopicManager extends BaseManager implements EntityManagerInterface
protected $counters;
+ public function flushNow()
+ {
+ parent::flushNow();
+
+ $user = $this->container->get('security.context')->getToken()->getUser();
+
+ $this->container->get('ccdn_forum_forum.registry.manager')->updateCachePostCountForUser($user);
+ }
+
+
+
/**
*
* @access public
@@ -100,6 +100,12 @@ class Post
*/
protected $attachment;
+ /**
+ * @ORM\ManyToOne(targetEntity="CCDNForum\ForumBundle\Entity\Registry", cascade={"persist"})
+ * @ORM\JoinColumn(name="registry_id", referencedColumnName="id", onDelete="SET NULL")
+ */
+ protected $registry;
+
/**
* Get id
*
@@ -517,4 +523,26 @@ public function getAttachment()
{
return $this->attachment;
}
+
+ /**
+ * Set registry
+ *
+ * @param CCDNForum\ForumBundle\Entity\Registry $registry
+ * @return Post
+ */
+ public function setRegistry(\CCDNForum\ForumBundle\Entity\Registry $registry = null)
+ {
+ $this->registry = $registry;
+ return $this;
+ }
+
+ /**
+ * Get registry
+ *
+ * @return CCDNForum\ForumBundle\Entity\Registry
+ */
+ public function getRegistry()
+ {
+ return $this->registry;
+ }
}
@@ -0,0 +1,101 @@
+<?php
+
+/*
+ * This file is part of the CCDNForum ForumBundle
+ *
+ * (c) CCDN (c) CodeConsortium <http://www.codeconsortium.com/>
+ *
+ * Available on github <http://www.github.com/codeconsortium/>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace CCDNForum\ForumBundle\Entity;
+
+use Doctrine\ORM\Mapping as ORM;
+use Doctrine\Common\Collections\ArrayCollection;
+use Symfony\Component\Validator\Constraints as Assert;
+
+use CCDNUser\UserBundle\Entity\User;
+
+/**
+ * @ORM\Entity(repositoryClass="CCDNForum\ForumBundle\Repository\RegistryRepository")
+ * @ORM\Table(name="CC_Forum_Registry")
+ */
+class Registry
+{
+
+ /**
+ * @ORM\Id
+ * @ORM\Column(type="integer")
+ * @ORM\GeneratedValue(strategy="AUTO")
+ */
+ protected $id;
+
+ /**
+ * @ORM\ManyToOne(targetEntity="CCDNUser\UserBundle\Entity\User", cascade={"persist"})
+ * @ORM\JoinColumn(name="owned_by_user_id", referencedColumnName="id", onDelete="SET NULL")
+ */
+ protected $owned_by;
+
+ /**
+ * @ORM\Column(type="integer")
+ */
+ protected $cachePostCount;
+
+
+ /**
+ * Get id
+ *
+ * @return integer
+ */
+ public function getId()
+ {
+ return $this->id;
+ }
+
+ /**
+ * Set cachePostCount
+ *
+ * @param integer $cachePostCount
+ * @return Registry
+ */
+ public function setCachePostCount($cachePostCount)
+ {
+ $this->cachePostCount = $cachePostCount;
+ return $this;
+ }
+
+ /**
+ * Get cachePostCount
+ *
+ * @return integer
+ */
+ public function getCachePostCount()
+ {
+ return $this->cachePostCount;
+ }
+
+ /**
+ * Set owned_by
+ *
+ * @param CCDNUser\UserBundle\Entity\User $ownedBy
+ * @return Registry
+ */
+ public function setOwnedBy(\CCDNUser\UserBundle\Entity\User $ownedBy = null)
+ {
+ $this->owned_by = $ownedBy;
+ return $this;
+ }
+
+ /**
+ * Get owned_by
+ *
+ * @return CCDNUser\UserBundle\Entity\User
+ */
+ public function getOwnedBy()
+ {
+ return $this->owned_by;
+ }
+}
@@ -25,6 +25,7 @@
*/
class PostRepository extends EntityRepository
{
+
/**
@@ -58,11 +59,12 @@ public function findPostForEditing($post_id)
try {
return $query->getsingleResult();
} catch (\Doctrine\ORM\NoResultException $e) {
- return;
+ return null;
}
}
+
/**
*
* for moderator
@@ -86,4 +88,30 @@ public function findLockedPostsForModeratorsPaginated()
}
}
+
+
+ /**
+ *
+ *
+ */
+ public function getPostCountForUserById($user_id)
+ {
+
+ $query = $this->getEntityManager()
+ ->createQuery('
+ SELECT COUNT(p.id) AS postCount
+ FROM CCDNForumForumBundle:Post p
+ LEFT JOIN p.topic t
+ WHERE p.created_by = :id AND p.deleted_by IS NULL AND t.deleted_by IS NULL')
+ ->setParameter('id', $user_id);
+
+ try {
+ return $query->getsingleResult();
+ } catch (\Doctrine\ORM\NoResultException $e) {
+ return null;
+ }
+
+ }
+
+
}
@@ -0,0 +1,51 @@
+<?php
+
+/*
+ * This file is part of the CCDNForum ForumBundle
+ *
+ * (c) CCDN (c) CodeConsortium <http://www.codeconsortium.com/>
+ *
+ * Available on github <http://www.github.com/codeconsortium/>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace CCDNForum\ForumBundle\Repository;
+
+use Doctrine\ORM\EntityRepository;
+
+/**
+ * RegistryRepository
+ *
+ *
+ * @author Reece Fowell <reece@codeconsortium.com>
+ * @version 1.0
+ */
+class RegistryRepository extends EntityRepository
+{
+
+
+ /**
+ *
+ * @access public
+ * @param int $folder_id
+ */
+ public function findRegistryRecordForUser($user_id)
+ {
+ $query = $this->getEntityManager()
+ ->createQuery('
+ SELECT r
+ FROM CCDNForumForumBundle:Registry r
+ WHERE r.owned_by = :id
+ ')
+ ->setParameter('id', $user_id);
+
+ try {
+ return $query->getSingleResult();
+ } catch (\Doctrine\ORM\NoResultException $e) {
+ return;
+ }
+ }
+
+}
@@ -39,6 +39,8 @@ public function findOneByIdJoinedToPostsPaginated($topic_id)
->createQuery('
SELECT t, p FROM CCDNForumForumBundle:Topic t
INNER JOIN t.posts p
+ LEFT JOIN p.created_by u
+ LEFT JOIN p.registry r
WHERE t.id = :id
GROUP BY p.id
ORDER BY p.created_date ASC')
@@ -65,6 +67,8 @@ public function findOneByIdJoinedToPosts($topic_id)
->createQuery('
SELECT t, p FROM CCDNForumForumBundle:Topic t
INNER JOIN t.posts p
+ LEFT JOIN p.created_by u
+ LEFT JOIN p.registry r
WHERE t.id = :id
GROUP BY p.id
ORDER BY p.created_date ASC')
@@ -1,4 +1,7 @@
parameters:
+ ccdn_forum_forum.registry.repository.class: CCDNForum\ForumBundle\Repository\RegistryRepository
+ ccdn_forum_forum.registry.manager.class: CCDNForum\ForumBundle\Entity\Manager\RegistryManager
+
ccdn_forum_forum.post.manager.class: CCDNForum\ForumBundle\Entity\Manager\PostManager
ccdn_forum_forum.post.form.type.class: CCDNForum\ForumBundle\Form\Type\PostType
ccdn_forum_forum.post.form.insert.handler.class: CCDNForum\ForumBundle\Form\Handler\PostInsertFormHandler
@@ -24,6 +27,18 @@ parameters:
ccdn_forum_forum.board.manager.class: CCDNForum\ForumBundle\Entity\Manager\BoardManager
services:
+ ccdn_forum_forum.registry.repository:
+ class: %ccdn_forum_forum.registry.repository.class%
+ factory_service: doctrine.orm.default_entity_manager
+ factory_method: getRepository
+ arguments:
+ - 'CCDNForum\ForumBundle\Entity\Registry'
+
+ ccdn_forum_forum.registry.manager:
+ class: %ccdn_forum_forum.registry.manager.class%
+ arguments: [@doctrine, @service_container]
+
+
ccdn_forum_forum.post.manager:
class: %ccdn_forum_forum.post.manager.class%
arguments: [@doctrine, @service_container]
Oops, something went wrong.

0 comments on commit e49ceb9

Please sign in to comment.