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

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
    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

    点赞 评论

相关推荐