25

2つの異なるエンティティからの値が必要です。やり方がわかりません。私はこれまでこれを試しました:

<?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);
    }

}

動作しません助けてください。ありがとう

4

2 に答える 2

49

EntityManagerあなたは呼び出すことによってアクセスすることができますDoctrine\ORM\EntityRepository#getEntityManager()

$repository = $this
    ->getEntityManager()
    ->getRepository('PondGeolocBundle:User_Lake');
于 2013-03-17T22:57:56.930 に答える
0

依存関係をさらに注入したい場合は、リポジトリをサービスとして宣言して、一方を注入してもう一方のリポジトリ内で使用できるようにします。

services.yml

services:
    repository.user_lake:
        class: Pond\GeolocBundle\Entity\UserLakeRepository
        factory: [@doctrine, getRepository]
        arguments:
            - PondGeolocBundle:User_Lake

    repository.pond_lake:
        class: Pond\GeolocBundle\Entity\PondLakeRepository
        factory: [@doctrine, getRepository]
        arguments:
            - PondGeolocBundle:PondLake
        calls:
            - [setUserLakeRepository, [@repository.user_lake]]

PondLakeRepository.phpでは、リポジトリ($ userLakeRepository)を格納するためのプロパティへのセッター(setUserLakeRepository)が必要です。

于 2017-02-08T15:39:18.840 に答える