简体   繁体   English

在Doctrine2中的5个表的多个联接中计数和显示相关记录

[英]Count and display related records in multiple joins of 5 tables in Doctrine2

I want to display names and count its related values in multiple related join. 我想显示名称并在多个相关联接中计算其相关值。 I want to loop the result like this 我想像这样循环结果

$datas = array();//initialise array
        foreach ($island as $is) {
            $datas[] = array($is->getName(),$is->getNums() );
        }

where $is->getNums() is the retrieved total count of voters for each island name 其中$ is-> getNums()是每个岛名的已获取选民总数

so far I tried this 到目前为止,我尝试了

  $island=$this->em->createQueryBuilder();
  $island->select('count(v.id) as getNums')
  ->from('DuterteBundle:Voters','v')
  ->join('v.city','c')
  ->join('c.province','p')
  ->join('p.region','r')
  ->join('r.island','i')
   $count = $island->getQuery()->getSingleScalarResult(); 
return $count;

voters is related to city, city is related to province, province is related to region, region is related to island.I want to display each island's name as well count and display all voters in each island's name 选民与城市有关,城市与省有关,省与地区有关,地区与岛屿有关。我要显示每个岛屿的名称以及计数并显示每个岛屿的名称中的所有选民

My table structures looks like this 我的表结构看起来像这样

  id name city_id //voters table
  id name province_id //city
  id name region_id // province
  id name island_id // region
  id name //island  

I can display all island name by simply using this function 我只需使用此功能即可显示所有岛屿名称

$island = $em->getRepository('Bundle:Island')->findAll();

which results in 导致

  array(4) {
    [0]=>
    array(1) {
     ["name"]=>
    string(5) "Luzon"
  }
    [1]=>
     array(1) {
       ["name"]=>
       string(8) "Mindanao"
 }

But I also want to count and get all voters in each Island.Any Ideas? 但是我也想计算并吸引每个岛上的所有选民。

Update 更新

I decided to create a service for this since this project is using Doctrine query 'heavily'..I tested this to create a custom filter in my twig.This method will count all voters per island name, just what I wanted above. 我决定为此创建服务,因为该项目使用“大量”的学说查询。我测试了此操作,以在树枝上创建自定义过滤器。此方法将按岛名计算所有选民的投票人数,正好是我上面想要的。

 my custome twig filter

 <?php

 namespace Project\Bundle\DuterteBundle\Twig;

 class AppExtension extends \Twig_Extension
 {
 protected $em;

 public function __construct($em)
 {
  $this->em = $em;
 }

 public function getFunctions()
{
  return array(
   //this is the name of the function you will use in twig
   new \Twig_SimpleFunction('number_votes', array($this, 'a'))
 );
}

 public function getName()
{

 return 'app.extension';
}   

public function a($id)
{
$qb=$this->em->createQueryBuilder();
$qb->select('count(v.id)')
  ->from('DuterteBundle:Voters','v')
  ->join('v.city','c')
  ->join('c.province','p')
  ->join('p.region','r')
  ->join('r.island','i')
  ->where('i.id = :x')
  ->setParameter('x',$id);
   $count = $qb->getQuery()->getSingleScalarResult(); 
  return $count;
 }
}

In services.yml 在services.yml中

 duterte.twig.app_extension:
    class: Project\Bundle\DuterteBundle\Twig\AppExtension
    tags:
        - { name: twig.extension }
    arguments:
        em: "@doctrine.orm.entity_manager"  

now in template 现在在模板中

  {% for island in island %}
    {{ number_votes(island.id) }}
  {% endfor %}

  outputs
  mindanao= 500,
  visayas =  670;
  so on....

Perfect..This is what I really wanted in the controller.To display island names as well retrieve counts per island name.Now Is it posible to use this in controller? 完美..这是我在控制器中真正想要的。要显示孤岛名称以及检索每个孤岛名称的计数。现在可以在控制器中使用它吗?

I tried this in controller 我在控制器中尝试过

 $no = $this->container->get('duterte.twig.app_extension');

 datas = array();//initialise array
        foreach ($island as $is) {
             //$no = $this->container->get('duterte.twig.app_extension');
            $datas[] = array($is->getName(),$no->number_votes($is->getId()));
        }

But i cant use the method 但我不能使用该方法

Attempted to call an undefined method named "number_votes" of class "Project\\Bundle\\DuterteBundle\\Twig\\AppExtension". 尝试调用名为“ Project \\ Bundle \\ DuterteBundle \\ Twig \\ AppExtension”的类的未定义方法“ number_votes”。

Got it finally..After spending hours trying to make this code to work, I finally got the solution. 终于明白了。花了数小时试图使此代码正常工作之后,我终于找到了解决方案。

First, I haven't found a solution in counting values a multiple join in 5 tables in Doctrine2. 首先,在Doctrine2的5个表中,我还没有找到对多个联接中的值进行计数的解决方案。 So creating a service is my final option.I created a Twig extension and call it inside the controller. 所以创建服务是我的最后选择。我创建了一个Twig扩展,并在控制器内部调用它。

   $island = $em->getRepository('DuterteBundle:Island')->createQueryBuilder('i')
       ->orderBy('i.name', 'ASC')
       ->getQuery()
       ->getResult();

My Twig extension as services looks exactly like this 我的Twig扩展服务就像这样

   public function a($id)
{
    $em = $this->getDoctrine()->getManager();
    $qb=$em->createQueryBuilder('i');
    $qb->select('count(v.id)')
      ->from('DuterteBundle:Voters','v')
      ->join('v.city','c')
      ->join('c.province','p')
      ->join('p.region','r')
      ->join('r.island','i')
      ->where('i.id = :x')
      ->setParameter('x',$id);
    $count = $qb->getQuery()->getSingleScalarResult(); 
    return $count;
}

Now inside the controller 现在在控制器内部

   $dat = $this->container->get('app_extension');
    $datas = array();//initialise array
        foreach ($island as $is) {
            $datas[] = array($is->getName(),$dat->a($is->getId()));
        }

Whoops, no errors but does not display any result. 糟糕,没有错误,但未显示任何结果。

So casting as integer will make this finally work , so 因此,将其强制转换为整数将使其最终起作用,因此

$dat = $this->container->get('app_extension');
    $datas = array();//initialise array
        foreach ($island as $is) {
            $datas[] = array($is->getName(),(int)$dat->a($is->getId()));//here
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM