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

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

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效