Calling an entity's setter for updating its co

2019-09-08 23:47发布

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?

1条回答
迷人小祖宗
2楼-- · 2019-09-09 00:44

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:

namespace PanelBundle\Controller;

use PanelBundle\Entity\Budget;
use PanelBundle\Form\Type\BudgetType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class BudgetController extends Controller
{
    public function addAction(Request $request)
    {
        $budget = new Budget();
        $budgetForm = $this->createForm(new BudgetType(), $budget);
        $budgetForm->handleRequest($request);

        if ($budgetForm->isValid()) {
            $manager = $this->getDoctrine()->getManager();
            $manager->persist($budget);
            $manager->flush();

            $client = $manager->getRepository('PanelBundle:Client')
                ->find($budgetForm->get('client_id')->getData())
            ;
            $client->addBudget($budget);
            $manager->flush();

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

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

        return $this->render(
            'PanelBundle:Budget:add.html.twig',
            array(
                'budgetForm' => $budgetForm->createView(),
            )
        );
    }
}
查看更多
登录 后发表回答