Skip to content

Instantly share code, notes, and snippets.

@jesusOmar
Created July 17, 2018 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jesusOmar/002f2caa4c2d9ca3b788f4cf0211ffdb to your computer and use it in GitHub Desktop.
Save jesusOmar/002f2caa4c2d9ca3b788f4cf0211ffdb to your computer and use it in GitHub Desktop.
Doctrine Caching Results?
<?php
namespace Stanford\BerriesBundle\Entity\Berry;
use Doctrine\ORM\EntityRepository;
/**
* CategoryRepository.
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class CategoryRepository extends EntityRepository
{
public function findUserActivities($user, $category = null)
{
$year = DateTime::now()->format('Y');
$query = $this->createQueryBuilder('c')
->select('c, a, ua, u, s')
->leftJoin('c.activities', 'a')
->leftJoin('a.userActivities', 'ua')
->leftJoin('ua.user', 'u')
->leftJoin('ua.status', 's')
->leftJoin('a.parentActivity', 'p')
->where('ua.startedOn >= :startDate')
->andWhere('u.id = :userId')
->andWhere('c.active = true')
->andWhere('a.active = true')
->andWhere('p.id = 3')
->setParameter('userId', $user->getId())
->setParameter('startDate', "{$year}-01-01")
;
if ($category) {
$query
->andWhere('c.slug = :category')
->setParameter('category', $category)
;
}
$query->orderBy('ua.completedOn', 'ASC');
return $query->getQuery()->getResult();
}
}
<?php
namespace AppBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class DefaultController extends Controller
{
/**
* @Route("/", name="home")
* @Method({"GET"})
*
* @Template("index.html.twig")
*/
public function indexAction()
{
$user = $this->getDoctrine()->getRepository(User::class)->find(26236);
$userActivities = $this->getDoctrine()->getRepository(Category::class)->findUserActivities($user);
echo "User should be: {$user->getId()}<br/><br/>";
foreach ($userActivities as $category) {
foreach ($category->getActivities() as $activity) {
foreach ($activity->getUserActivities() as $userActivity) {
echo "UA.id: {$userActivity->getId()} U.id: {$userActivity->getUser()->getId()} <br/>";
}
}
}
$user = $this->getDoctrine()->getRepository(User::class)->find(25845);
$userActivities2 = $this->getDoctrine()->getRepository(Category::class)->findUserActivities($user);
echo "<br/>User should be: {$user->getId()}<br/><br/>";
foreach ($userActivities2 as $category) {
foreach ($category->getActivities() as $activity) {
foreach ($activity->getUserActivities() as $userActivity) {
echo "UA.id: {$userActivity->getId()} U.id: {$userActivity->getUser()->getId()} <br/>";
}
}
}
die;
}
}
User should be: 26236
UA.id: 836868 U.id: 26236
UA.id: 836784 U.id: 26236
UA.id: 836842 U.id: 26236
UA.id: 837069 U.id: 26236
UA.id: 860227 U.id: 26236
UA.id: 862047 U.id: 26236
UA.id: 867600 U.id: 26236
UA.id: 875389 U.id: 26236
User should be: 25845
UA.id: 860227 U.id: 26236
UA.id: 862047 U.id: 26236
UA.id: 867600 U.id: 26236
UA.id: 875389 U.id: 26236
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment