在使用 Symfony2 和 Doctrine2 插入另一个表后尝试更新一个表

2022-01-03 00:00:00 orm php symfony doctrine-orm query-builder

我在 BudgetRepository 中编写了一个函数,该函数在将新数据插入预算表时调用.功能是:

I wrote a function in BudgetRepository that is called when inserting new data into Budget table. The function is:

public function addBudgetToClient($clientId, $budgetId)
{
    return $this->createQueryBuilder('b')
                ->update('PanelBundle:Client', 'c')
                ->set('c.budget', $budgetId)
                ->where('c.id = ' . $clientId)
                ->getQuery()
                ->execute();
}

BudgetController 就是这样做的:

public function addAction(Request $request)
{
    $form = $this->createForm(new BudgetType());
    $manager = $this->getDoctrine()->getManager();
    $Budget = $manager->getRepository('PanelBundle:Budget');
    $Client = $manager->getRepository('PanelBundle:Client');
    
    if ($request->getMethod() == 'POST') {
        $form->handleRequest($request);
        
        if ($form->isValid()) {
            $manager->persist($form->getData());
            $manager->flush();
            // Here's the method:
            $Budget->addBudgetToClient($form['client_id']->getData()->getId(), $Budget->getLastId());

            //
            $this->addFlash('success', 'Novo orçamento adicionado');
            
            return $this->redirect($this->generateUrl('panel_budgets'));
        }
    }
    
    return $this->render('PanelBundle:Budget:add.html.twig', array(
        'clients' => $Client->findAll(),
        'form' => $form->createView()
    ));
}

我测试了两个输出,getLastId 也是我编写的一个自定义函数,用于从 Budget 中检索最大 ID,以及 $form['client_id']->getData()->getId() 还检索客户端 ID.我猜Symfony2会自动做一些事情,因为Budget和Client是相关的,甚至保存Client id,在数据库中显示Client名称,我不明白实际上是怎么回事.

I tested both outputs, the getLastId is also a custom function I wrote to retrieve the biggest ID from Budget, and the $form['client_id']->getData()->getId() also retrieves the Client id. I guess Symfony2 automatically does something because Budget and Client are related, and even saving the Client id, in the database shows the Client name, I don't understand how actually.

这里的问题是这些错误:

The issue here are these errors:

[语义错误] 第 0 行,第 34 列靠近budget = 4 WHERE":错误:无效的 PathExpression.应为 StateFieldPathExpression 或 SingleValuedAssociationField.

[Semantical Error] line 0, col 34 near 'budget = 4 WHERE': Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected.

[2/2] QueryException: [Semantical Error] line 0, col 34 near 'budget = 4 WHERE': Error: Invalid PathExpression.预期的 StateFieldPathExpression 或 SingleValuedAssociationField.+

[2/2] QueryException: [Semantical Error] line 0, col 34 near 'budget = 4 WHERE': Error: Invalid PathExpression. StateFieldPathExpression or SingleValuedAssociationField expected. +

[1/2] QueryException: UPDATE PanelBundle:Client c SET c.budget = 4 WHERE c.id = 1 +

[1/2] QueryException: UPDATE PanelBundle:Client c SET c.budget = 4 WHERE c.id = 1 +

我发现这个异常有很多问题,但是update函数没有,只有select.

I found many problems about this exception but they haven't had it with update function, only select.

推荐答案

您不应使用 queryBuilder 为这种情况构建更新查询.使用 OOP 方法更新您的实体.

You should not build an update query for this case using a queryBuilder. Use OOP approach to update your entities.

if ($form->isValid()) {
    $budgetEntity = $form->getData();
    $manager->persist($budgetEntity);
    $clientEntity = $Budget->find($form['client_id']->getData()->getId());
    $clientEntity->setBudget($budgetEntity);

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

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

相关文章