1

For example, consider front page as in Jobeet tutorial:

class CategoriesRepository extends EntityRepository {

  public function getWithJobs($limit)
  {
    $categories = $this->getContainingJobs();
    $jobsRepo = $this->em->getRepository("JobeetBundle:Jobs");
    foreach($categories as $c) {
      $c->setActiveJobs($jobsRepo->getActiveJobsByCategory($c->id, $limit);
    }
   return $categories;
  }

}

So inside controller I dont' have to use service layer for usage of both repositories.

Could someone give me any advice?

keram
  • 2,321
  • 4
  • 24
  • 29

1 Answers1

1

If you have defined an association between your categories and jobs, you shall not have to call another repository. You get your categories entity with related jobs already set by joining them in your DQL query...

Here is the official documentation for this case : http://symfony.com/doc/current/book/doctrine.html#joining-to-related-records

And the example coming from this doc :

// src/Acme/StoreBundle/Repository/ProductRepository.php

public function findOneByIdJoinedToCategory($id)
{
    $query = $this->getEntityManager()
        ->createQuery('
            SELECT p, c FROM AcmeStoreBundle:Product p
            JOIN p.category c
            WHERE p.id = :id'
        )->setParameter('id', $id);

    try {
        return $query->getSingleResult();
    } catch (\Doctrine\ORM\NoResultException $e) {
        return null;
    }
}
AlterPHP
  • 12,667
  • 5
  • 49
  • 54
  • I can't do it this way. I want to get 10 records by single category, so I need to call SQL query for every category. Similar like it is in Symfony 1 Jobeet tutorial. All I want to is avoid using service layer to merge active jobs with category. – keram May 09 '12 at 12:25
  • IMHO if Repository clas allow me to use EM inside I can use another repo. I want to just be sure it's good design practice. – keram May 09 '12 at 12:31
  • You can do it. It doesn't seem to be a BAD practice as you can use EntityManager from Repository. – AlterPHP May 09 '12 at 13:06