如何从Doctrine FindAll()结果中填充值

时间:2015-09-04 03:33:30

标签: php symfony doctrine-orm

我想构建动态数据来自数据库的图表。 到目前为止,我可以通过创建存储在数组中的虚拟数据来显示此图表。

控制器

//dummy data
$data = array(
        array('OFW', 45.0),
        array('Mindanao', 26.8),
        array('Visayas', 12.8),
        array('Luzon', 8.5),
    );

现在我想用数据库中的实际值填充这些值。我正在为这个项目使用Doctrine2

 $island = $em->getRepository('DuterteBundle:Island')->findAll();
 foreach($island as $is) { return $is }
 //where $is = island1, island2, island3

然后我重构之前的虚拟数据,如

  $data = array(array($is, 67));

  return $this->render('Bundle:.........);

此代码仅显示只有一个$ is值的图形。在这种情况下,island1.Wey是显示所有值的正确方法吗?

更新

我重构了我的代码......

// controller
 $island = $em->getRepository('DuterteBundle:Island')->createQueryBuilder('i')
       ->orderBy('i.name', 'ASC')
       ->getQuery()
       ->getResult();
    $island_names = array();//initialise array
        foreach ($island as $is) {
            $island_names[] = array(
                'name' => $is->getName()
            );
        }   

  $val = array();
    foreach ($island_names as $key) {
        $val = $key['name'];
    }
    $data = array(array($val,56.78));

转储数据

 echo '<pre>';
    \Doctrine\Common\Util\Debug::dump($data);
    echo '</pre>';

结果

 array(1) {
    [0]=>
    array(2) {
      [0]=>
      string(7) "Visayas"
      [1]=>
     float(56.78)
  }
}

更新 我重新重构了我的代码

 $island = $em->getRepository('DuterteBundle:Island')->createQueryBuilder('i')
       ->orderBy('i.name', 'ASC')
       ->getQuery()
       ->getResult();
    $datas = array();//initialise array
        foreach ($island as $is) {
            $datas[] = array(
                'name' => $is->getName(),
                'id' => 100
            );
        }

现在转储数据将显示所有预期结果

array(4) {
  [0]=>
  array(2) {
    ["name"]=>
    string(5) "Luzon"
      ["id"]=>
      int(100)
 }
  [1]=>
    array(2) {
     ["name"]=>
     string(8) "Mindanao"
     ["id"]=>
        int(100)
  }
  [2]=>
    array(2) {
     ["name"]=>
     string(3) "OFW"
       ["id"]=>
       int(100)
 }
 [3]=>
    array(2) {
    ["name"]=>
      string(7) "Visayas"
        ["id"]=>
        int(100)
  }
}

现在剩下的问题是填充到图表所需的数组

虚拟数据看起来像这样

$data = array(
        array('OFW', 45.0),
        array('Mindanao', 26.8),
        array('Visayas', 12.8),
        array('Luzon', 8.5),
    );

我试图用这样的真实值替换

 $data = array(array($datas));

模板中的图表没有显示。我可以看到转储的值。

更新

图形控制器中的整个代码

public function graphAction(Request $request)
{
    //we will count all voters here
    $em = $this->getDoctrine()->getManager();
    $voters = $em->getRepository('DuterteBundle:Voters')->findAll();
    $voters_count = count($voters);

    $ob = new Highchart();
    $ob->chart->renderTo('piechart');
    $ob->title->text("Island/OFW voter's shares"." ".(number_format($voters_count))."total votes as of " . date('F d,Y h:i A')."/Asia-Manila Time");
    $ob->plotOptions->pie(array(
        'allowPointSelect'  => true,
        'cursor'    => 'pointer',
        'dataLabels'    => array('enabled' => false),
        'showInLegend'  => true
    ));

    $island = $em->getRepository('DuterteBundle:Island')->createQueryBuilder('i')
       ->orderBy('i.name', 'ASC')
       ->getQuery()
       ->getResult();
    $datas = array();//initialise array
        foreach ($island as $is) {
            $datas[] = array(
                'name' => $is->getName(),
                'id' => 100
            );
        }
     echo '<pre>';
    \Doctrine\Common\Util\Debug::dump($datas);
    echo '</pre>';
    $data = array(array($datas));//the problem is here

    //with this dummy data, the chart/graph will work

    /*$data = array(//how to replace this with real datas?
        array('OFW', 45.0),
        array('Mindanao', 26.8),
        array('Visayas', 12.8),
        array('Luzon', 8.5),
    );*/
    $series = array(
        array('type' => 'pie','name' => "Voter's share", 'data' => $data)
    );
    $ob->series($series);
    return $this->render('GraphBundle:Default:graph.html.twig', array(
        'chart' => $ob,
        'votercount' => $voters_count
    ));

}

2 个答案:

答案 0 :(得分:0)

如果结果中有很多名称并想要计算它们,可以使用array_count_values PHP函数:

Read

如果你想按照描述格式化它:

$data = array();
foreach ($island as $is) {
    $data[] = $is->getName();
};

$data = array_count_values($data);

答案 1 :(得分:0)

我明白了。我将foreach循环更改为

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

根据用户malcolm的建议