Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get foreign repository inside my repository in Doctrine2/Symfony2?

I need values from 2 different entities. I don't know how to do. I tried this so far:

<?php

namespace Pond\GeolocBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * PondLakeRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class PondLakeRepository extends EntityRepository
{

    public function getSwimsAvailableById($id)
    {
        // get the nb of swims of a lake
        $lake = $this->findOneById($id);
        $swims = $lake->getSwims();

        $repository = $this->getDoctrine()
                           ->getManager()
                           ->getRepository('PondGeolocBundle:User_Lake');

        //get the nb of users in a lake
        $qb = $this->_em->createQueryBuilder();
        $qb->select('count(a.id)');
        $qb->from('PondGeolocBundle:User_Lake', 'a');

        $nbOfUsers = $qb->getQuery()->getSingleScalarResult();

        // return the nb of swims available onthis lake
        $avail = $swims - $nbOfUsers;
        print_r ($avail);
    }

}

Doesn't work Please help. Thanks

like image 400
Miles M. Avatar asked Mar 17 '13 21:03

Miles M.


1 Answers

You can access the EntityManager by calling Doctrine\ORM\EntityRepository#getEntityManager():

$repository = $this
    ->getEntityManager()
    ->getRepository('PondGeolocBundle:User_Lake');
like image 53
Sgoettschkes Avatar answered Oct 23 '22 06:10

Sgoettschkes