Commits

Vasiliy Toporov  committed 58be248

Refactored method for active Jobs on homepage.

  • Participants
  • Parent commits 216e287

Comments (0)

Files changed (7)

File src/Ens/JobeetBundle/Controller/JobController.php

     {        
         $em = $this->getDoctrine()->getEntityManager();
  
-        $query = $em->createQuery(
-          'SELECT j FROM EnsJobeetBundle:Job j WHERE j.expires_at > :date'
-        )->setParameter('date', date('Y-m-d H:i:s', time() - 86400 * 30));
-        $entities = $query->getResult();
+        $entities = $em->getRepository('EnsJobeetBundle:Job')->getActiveJobs();
 
         return $this->render('EnsJobeetBundle:Job:index.html.twig', array(
           'entities' => $entities

File src/Ens/JobeetBundle/Entity/Affiliate.php~

     {
         $this->created_at = new \DateTime();
     }
-}
+}

File src/Ens/JobeetBundle/Entity/Category.php~

     {
         return $this->getName();
     }
-}
+}

File src/Ens/JobeetBundle/Entity/CategoryAffiliate.php~

     {
         return $this->affiliate;
     }
-}
+}

File src/Ens/JobeetBundle/Entity/Job.php~

     {
         return Jobeet::slugify($this->getLocation());
     }
-}
+
+    /**
+     * @ORM\PrePersist
+     */
+    public function setExpiresAtValue()
+    {
+        if(!$this->getExpiresAt())
+        {
+            $now = $this->getCreatedAt() ? $this->getCreatedAt()->format('U') : time();
+            $this->expires_at = new \DateTime(date('Y-m-d H:i:s', $now + 86400 * 30));
+        }
+    }
+}

File src/Ens/JobeetBundle/Repository/JobRepository.php

+<?php
+
+namespace Ens\JobeetBundle\Repository;
+
+use Doctrine\ORM\EntityRepository;
+
+/**
+ * JobRepository
+ *
+ * This class was generated by the Doctrine ORM. Add your own custom
+ * repository methods below.
+ */
+class JobRepository extends EntityRepository
+{
+    public function getActiveJobs($category_id = null)
+    {
+        $qb = $this->createQueryBuilder('j')
+          ->where('j.expires_at > :date')
+          ->setParameter('date', date('Y-m-d H:i:s', time()))
+          ->orderBy('j.expires_at', 'DESC');
+
+        if($category_id)
+        {
+            $qb->andWhere('j.category = :category_id')
+                ->setParameter('category_id', $category_id);
+        }
+
+        $query = $qb->getQuery();
+
+        return $query->getResult();
+    }
+}

File src/Ens/JobeetBundle/Resources/config/doctrine/Job.orm.yml

 # src/Ens/JobeetBundle/Resources/config/doctrine/Job.orm.yml
 Ens\JobeetBundle\Entity\Job:
   type: entity
+  repositoryClass: Ens\JobeetBundle\Repository\JobRepository
   table: job
   id:
     id: