I need to update the Client table's budget column after inserting a new Budget into the database, but it doesn't. This is how I am doing inside of BudgetController::addAction()
:
if ($form->isValid()) {
$manager->persist($form->getData());
$manager->flush();
$Client = $manager->getReference('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'));
}
The $Client
declaration returns the Client name successfully, but the line where it sets the setBudget()
seem not to work. I don't know how to make an update like this. I need it to update after inserting into Budget according to the selected Client id in Budget form.
Client and Budget are related to oneToMany
and manyToOne
, respectively, am I missing something because of this relationship?
If the Budget entity is a ManyToOne association of the Client, then you should be using
->addBudget()
instead of a setter. It's also probably better to do a->find()
for the Client entity instead of a->getReference()
. If you really want to save the extra trip to the database, use the setter on the Budget entity instead to set the$client
proxy created by the->getReference()
, i.e.$budget->setClient($client);
. But it's not that expensive to find the Client and it ensures that the Client of that id exists. It would then also be a good idea to flush the manager again, just to make sure things are wrapped up cleanly, instead of assuming it will all happen without interruption as the kernel terminates. A complete rendition of your controller and action should look something like this: