尝试在使用Symfony2和Doctrine2插入另一个表后更新一个表 [英] Trying to update one table after inserting into another one with Symfony2 and Doctrine2

查看:111
本文介绍了尝试在使用Symfony2和Doctrine2插入另一个表后更新一个表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 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();
}

而$ code BudgetController 这样做:

And the BudgetController does this:

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 也是一个自定义函数,用于从预算中检索最大的ID,而$ code> $ form ['client_id'] - > getData() - > getId()还检索客户端标识。我猜Symfony2会自动做一些事情,因为预算和客户端是相关的,甚至保存客户端ID,在数据库中显示客户端名称,我不明白如何实际。

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,col 34 near'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:[语义错误]行0,col 34 near'budget = 4 WHERE':错误:无效的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 +

我发现有关这个异常的许多问题,但他们没有使用更新函数,只有选择

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'));
}

这篇关于尝试在使用Symfony2和Doctrine2插入另一个表后更新一个表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆