如何在Doctrine2 / Symfony2中的存储库中获取外部存储库? [英] How to get foreign repository inside my repository in Doctrine2/Symfony2?

查看:120
本文介绍了如何在Doctrine2 / Symfony2中的存储库中获取外部存储库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要来自2个不同实体的值。我不知道该怎么办
我到目前为止尝试过:

 <?php 

命名空间池塘GeolocBundle\Entity;

使用Doctrine\ORM\EntityRepository;

/ **
* PondLakeRepository
*
*这个类是由Doctrine ORM生成的。在下面添加您自己的定制
*存储库方法。
* /
class PondLakeRepository扩展EntityRepository
{

public function getSwimsAvailableById($ id)
{
//获取游标的nb一个湖
$ lake = $ this-> findOneById($ id);
$ swims = $ lake-> getSwims();

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

//获取湖中的用户的nb
$ qb = $ this-> _em-> createQueryBuilder();
$ qb-> select('count(a.id)'); ('PondGeolocBundle:User_Lake','a');
$ qb->

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

//返回在这个湖上可用的游戏的nb
$ avail = $ swims - $ nbOfUsers;
print_r($ avail);
}

}

不工作
请帮忙
谢谢

解决方案

您可以通过调用访问 EntityManager 教义\ORM\EntityRepository#getEntityManager()

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


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

解决方案

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

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

这篇关于如何在Doctrine2 / Symfony2中的存储库中获取外部存储库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆