Unrecognised function

Asked

Viewed 74 times

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,
     );
}

You’re making the following mistake:

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 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 you make the same mistake.

  • Check if the line $prevista = $em->getRepository('RoqSysControlManutencaoBundle:Prevista')->findAll(); Returns a valid object.

  • Check that your entity (Entity) has the get method defined

  • @Adir Kunhn a Entity intervencao ou previsto????

  • @Catarinasilvestre in the $provided

  • 1

    Ahh, now that I’ve seen findAll() it will return an Array of Objects, then you have to do a foreach to give a get of each element

  • @Pedro Henrique the line is returning an object, is reporting the predicted.

  • @Adir Kuhn as??????

Show 2 more comments

2 answers

2

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

var_dump($prevista);

To get the values of $predicted you have to make a loop

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

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

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

Then only one entity will be returned and you can directly use the get

$prevista->getDescricao();
  • makes the same mistake.

  • had put $forecast in place of $obj

  • haven’t yet? put var_dump content in your question or Pastebin

0

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

Therefore, you need to iterate through this collection to then call the method getDescricao of the entity 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());
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.