drpjdfj618393 2018-03-08 07:02
浏览 54
已采纳

symfony 3.3和3.4 Doctrine Entity管理器进行数组转换

I have the following controller that gives the same problem on symfony 3.3 and 3.4 all with PHP 7. The controller works when I only have one element returned from DB. The problem is when the query returns more than one element. In that case it returns a Symfony error. This is the Controller's Action:

(...)
public function checkLastReportAction(){
    $em = $this->getDoctrine()
            ->getRepository('NCbrtBundle:NcBackupEvents')
            ->findServerByBackupReport();
    var_dump($em);
    $length = count($em);
    $TimeDifferenceAux = array();
    echo 'size: ' . $length . '<br>';
    for($i=0; $i <= $length-1; $i++){
        echo $i . '<br>';
        $format = 'Y-m-d H:i:s';
        $latest_date_str = $em[$i];
        $latest = date_create_from_format($format, $latest_date_str['latest']);
        (...)
        echo $TimeDifferenceAux . ' <-> ' . $em[$i]['frequency'] . '<br>';
        if ($TimeDifferenceAux > $em[$i]['frequency'] && 
                $em[$i]['frequency'] > 0 &&
                $em[$i]['frequency'] != ''){
            echo 'Report ' . $em[$i]['name'] . '<br>';

            $event = new NcBackupEvents();

            $event->setBackupmethod('BRT: No report');
            (...)
            $event->setSuccess('3');

            // Create the LOG
            $log = 'This server has a backup frequency of ' . 
            (...)
            $event->setLog($log);

            $server = $this->getDoctrine()
                    ->getRepository('NCbrtBundle:SrvrsServers')
                    ->find($em[$i]['id']);

            $event->setSrvrsServers($server);
            $em = $this->getDoctrine()->getManager();
            $em->persist($event);

            $em->flush();
            echo 'Saved new event with id ' . $event->getId() . '<br>';
        } else {
            echo 'No need to report<br>';                
        }
    }
    return new Response('Done.');
  }
}

This controller calls the following repository:

public function findServerByBackupReport(){
    $dql = 'SELECT s.name, s.id, MAX(n.dateCreated) latest, s.frequency 
        FROM NCbrtBundle:NcBackupEvents n
        JOIN n.srvrsServers s
        WHERE s.statusActive = 1 GROUP BY s.name';
    $query = $this->getEntityManager()->createQuery($dql);
    try {
        return $query->getResult();
    } catch (\Doctrine\ORM\NoResultException $e) {
        return null;
    }        
}

According to the documentation this query should return and array and it does, but Simfony complains about Cannot use object of type Doctrine\ORM\EntityManager as array. Check out the image: enter image description here I can not figure our what I am doing wrong. Can someone give me a clue?

  • 写回答

1条回答 默认 最新

  • duankousong9637 2018-03-08 08:12
    关注

    If you would have named your variables correctly you wouldn't have this bug.

    Your query result go in a variable named $em

    Then later in your loop you put the entity manager in it $em = $this->getDoctrine()->getManager();

    That's why it bugs after the first loop is complete

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?