Pregunta

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?

¿Fue útil?

Solución

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;
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top