Function not recognized

3

Hello, I have the following code:

  

Old code:

public function historicoAction() {
     $entity = new Intervencao();
     $em = $this->getDoctrine()->getManager();

     $entities = $em->getRepository('RoqSysControlManutencaoBundle:Intervencao')->findAll();
     $maquinas = $em->getRepository('RoqSysControlManutencaoBundle:Maquina')->findAll();

     $prevista = $em->getRepository('RoqSysControlManutencaoBundle:Prevista')->findAll();

     $entity->setDescricao($prevista->getDescricao());

     return array(
          'entity' => $entity,
          'entities' => $entities,            
          'maquinas' => $maquinas,
     );
}

It gives me the following error:

  

Error: Call to a member function getDescription () on a non-object in /var/www/roqsys/src/RoqSys/Control/ManutencaoBundle/Controller/IntervencaoController.php line 76

Edit **

  

New code:

I've tried the following:

public function historicoAction() {
    $entity = new Intervencao();
    $em = $this->getDoctrine()->getManager();

    $entities = $em->getRepository('RoqSysControlManutencaoBundle:Intervencao')->findAll();
    $maquinas = $em->getRepository('RoqSysControlManutencaoBundle:Maquina')->findAll();

    $prevista = $em->getRepository('RoqSysControlManutencaoBundle:Prevista')->findAll();
    var_dump($prevista);
    foreach($prevista as $obj) {
        echo $prevista->getDescricao();
    }
    $entity->setDescricao($prevista->getDescricao());

    return array(
        'entity' => $entity,
        'entities' => $entities,  
        'prevista' => $prevista,
        'maquinas' => $maquinas,
    );
}

And yet it gives me the same error.

    
asked by anonymous 25.02.2015 / 12:11

2 answers

2

The expected variable is possibly being returned as an object array, try checking with the var_dump

var_dump($prevista);

To get the values of 'predicted $' you have to loop

foreach($prevista as $obj) {
    echo $obj->getDescricao();
}

If you want to get only one element of this entity you need to have its id and use the

$prevista  = $em->getRepository('RoqSysControlManutencaoBundle:Prevista')->find($idPrevista);

So yes, only one entity will be returned and you can use get

$prevista->getDescricao();
    
25.02.2015 / 12:24
0

This error occurs because when you use the findAll method of EntityRepository (of Doctrine), it returns a collection of objects - or, more specifically, ArrayCollection . In your case, a ArrayCollection of objects of type Prevista are returned.

So, you need to iterate through this collection and then call the getDescricao method of the Prevista :

$entity = new Intervencao();
$em = $this->getDoctrine()->getManager();

$entities  = $em->getRepository('RoqSysControlManutencaoBundle:Intervencao')->findAll();
$maquinas  = $em->getRepository('RoqSysControlManutencaoBundle:Maquina')->findAll();
$previstas = $em->getRepository('RoqSysControlManutencaoBundle:Prevista')->findAll();

foreach ($previstas as $prevista) {
     $entity->setDescricao($prevista->getDescricao());
}
    
25.02.2015 / 13:04