调用实体的设置器,从另一个实体的控制器更新其列 [英] Calling an entity's setter for updating its column from a controller of another entity
问题描述
在将新的预算插入数据库后,我需要更新客户端表的预算列,但不支持。这是我在 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','Novoorçamentoadicionado');
return $ this-> redirect($ this-> generateUrl('panel_budgets'));
}
$ Client
声明返回客户端名称,但是设置 setBudget()
的行似乎不起作用。我不知道如何进行这样的更新。根据预算窗体中所选的客户端ID,我需要更新。预算中的客户端ID。
客户和预算与 oneToMany
和 manyToOne
,我是否因为这种关系而遗漏了一些东西?
如果Budget实体是客户端的ManyToOne关联,那么您应该使用 - > ; addBudget()
而不是setter。对于客户实体而不是 - > getReference()$ c $ - > find()
C>。如果您真的要保存额外的数据库,请使用Budget实体上的setter来设置由创建的代理
,即 $ client
> getReference() $ budget-> setClient($ client);
。但找到客户端并不昂贵,它确保该客户端的客户端存在。那么再次刷新管理员也是一个好主意,只是为了确保事情整齐地被包装,而不是假定它将全部发生在内核终止时不会中断。您的控制器和操作的完整重现应如下所示:
命名空间PanelBundle\Controller;
使用PanelBundle\Entity\Budget;
使用PanelBundle\Form\Type\BudgetType;
使用Symfony\Bundle\FrameworkBundle\Controller\Controller;
使用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','Novoorçamentoadicionado');
return $ this-> redirect($ this-> generateUrl('panel_budgets'));
}
return $ this-> render(
'PanelBundle:Budget:add.html.twig',
array(
'budgetForm' = $ budgetForm-> createView(),
)
);
}
}
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:
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(),
)
);
}
}
这篇关于调用实体的设置器,从另一个实体的控制器更新其列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!