dongyue934001 2015-09-07 15:04
浏览 30

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

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

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

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:

[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 or SingleValuedAssociationField expected. +

[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.

  • 写回答

1条回答 默认 最新

  • douyou2368 2015-09-07 15:56
    关注

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

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。