Created
July 17, 2018 13:48
-
-
Save jesusOmar/002f2caa4c2d9ca3b788f4cf0211ffdb to your computer and use it in GitHub Desktop.
Doctrine Caching Results?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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