Symfony2 - Refresh field of one entity when executing action on Controller of another entity

1

I need to update the budget field of my Client entity when entering new data into the Budget table. Both have relation oneToMany and manyToOne , respectively. I received suggestions to use the OO concept to do this operation, so something more or less was suggested to me:

if ($form->isValid()) {
    $manager->persist($form->getData());
    $manager->flush();

    $Client = $manager->find('PanelBundle:Client', $form['client_id']->getData()->getId());
    $Client->setBudget($manager->getRepository('PanelBundle:Budget')->getLastId());

    $this->addFlash('success', 'Novo orçamento adicionado');

    return $this->redirect($this->generateUrl('panel_budgets'));
}

When you see the output of $Client , when you assign the variable the find method, the respective name field of the id is displayed, which is the id of Client selected by select and passed to the variable when submitting the form.

Even though there are no errors, this field is not updated when you register. I've tried another way by writing a specific function to update, looking for examples, but I got invalid semantic errors.

How can I update the budget field when executing the Budget controller's addAction method?

    
asked by anonymous 08.09.2015 / 05:03

1 answer

1

Everything is fine in your code. You just forgot to keep $Client and give flush , just like you do with budget.

$Client->setBudget($manager->getRepository('PanelBundle:Budget')->getLastId());

$manager->persist($Client);
$manager->flush();
    
08.09.2015 / 15:12