php - 如何在 Doctrine2/Symfony2 的存储库中获取外部存储库?

标签 php symfony doctrine-orm repository

我需要来自 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);
    }

}

不起作用 请帮忙。 谢谢

最佳答案

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

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

关于php - 如何在 Doctrine2/Symfony2 的存储库中获取外部存储库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15466513/

相关文章:

php - Mysql SELECT from 2 表

javascript - 从 AJAX 响应程序脚本调用 PHP 类

php - 使用 Bootstrap 自定义 Symfony 2 表单

php - Symfony 3 - 我的实体存在 ManyToMany 问题

javascript - HTML 不想被执行

Doctrine 2 : What is wrong with the association between these entities?

php - 学说 2 dql 别名

java - 如何使用 httppost FileEntity 与 PHP 服务器一起上传文件

php - Symfony 中的 Postgres 部分唯一索引

php - 通过下拉菜单循环数字,但获取数字而不是字母