尝试将DQL查询放入Highcharts数组中,尽管填充了数组,但返回0

时间:2017-06-07 03:59:19

标签: php symfony highcharts bundle dql

以下是我的代码中查询的var_dump:

array (size=2)
  0 => 
    array (size=4)
  0 => 
    array (size=1)
      'barbell_curl_1_weight' => int 15
  1 => 
    array (size=1)
      'barbell_curl_1_weight' => int 20
  2 => 
    array (size=1)
      'barbell_curl_1_weight' => int 25
  3 => 
    array (size=1)
      'barbell_curl_1_weight' => int 20

这是我的疑问:

/**
 * @param User $user
 * @param String $col
 * @return array
 */
public function getStatsForStatsByDQL(User $user,  String $col)
{

    $parameters = (array('user' => $user,
    ));
    $em2 = $this->getDoctrine()->getManager()
        ->getRepository('AppBundle:ExerciseStats')
        ->createQueryBuilder('g')
        ->setParameters($parameters)
        ->where('g.user = :user')
        ->select('g.'. $col)
        ->setMaxResults(12)
        ->join('g.user', 'user')
        ->orderBy( 'g.'. 'timestamp','ASC')
        ->getQuery()->getResult(Query::HYDRATE_ARRAY);
    return $em2;
  }

从var_dump中可以看出,我得到了我需要的数据。这是Highcharts的功能:

/**
 * @return \Symfony\Component\HttpFoundation\Response
 * @Route ("user/stats", name="user_stats")
 */
public function chartAction()
{
    $user = $this->getUser();
    // Chart
    $query = $this->getStatsForStatsByDQL($user, 
ExerciseConsts::BARBELL_CURL_1_WEIGHT);


    $series = array($query, array("name" => "weight", "data" => 
$query));

    $ob = new Highchart();
    $ob->chart->renderTo('linechart');  
    $ob->title->text('Barbell Curl Weight');
    $ob->xAxis->title(array('text'  => "Workouts"));
    $ob->yAxis->title(array('text'  => "Weight"));
    $ob->series($series);

    return $this->render('user/stats.html.twig', array(
        'chart' => $ob
    ));
}

所以我回来的都是0.在xdebug中使用断点时,我得到了回复:

1 = {array} [2]
  name = "weight"
  data = {array} [4]
  0 = {array} [1]
    barbell_curl_1_weight = 15
  1 = {array} [1]
    barbell_curl_1_weight = 20
  2 = {array} [1]
    barbell_curl_1_weight = 25
  3 = {array} [1]
    barbell_curl_1_weight = 20

如何提取权重并将其输入Highchart图表?在这方面,完全没有关于Highcharts Bundle的文档。如果有人有任何线索,请告诉我。谢谢!

这是json_encode的var_dump($ series,JSON_NUMERIC_CHECK)。也许这会有所帮助:

字符串'[[{“barbell_curl_1_weight”:15},{“barbell_curl_1_weight”:20},{“barbell_curl_1_weight”:25},{“barbell_curl_1_weight”:20}],{“name”:“weight”,“数据“:[{”barbell_curl_1_weight“:15},{”barbell_curl_1_weight“:20},{”barbell_curl_1_weight“:25},{”barbell_curl_1_weight“:20}]}]'(长度= 266)

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。当我们处理一个多维数组时,Highcharts并没有向下钻取第一个数组,它返回0.这是我的迭代返回每个数组的值:

 $temp = array();
    foreach($array as $k=>$v){
        if(is_array($v)){
            foreach($v as $key=>$value){
                $temp[] = $value;
            }
        }
    }

所以现在,我只需要用$ query替换$ temp并且瞧瞧',图表将正确呈现所有值。