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 Stata 面板数据模型选择
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用