简体   繁体   English

如何使用symfony更新mysql列

[英]how to update mysql column using symfony

I created in view file html input form and I named it "thenum" 我在视图文件html输入表单中创建了一个表单,并将其命名为“ thenum”

{% extends 'base.html.twig' %}

{% block body %}
    <h1>User edit</h1>

    {{ form_start(edit_form) }}
        {{ form_widget(edit_form) }}
        <input type="text" name="thenum" value="0"><br>

        <input type="submit" value="Edit" />
    {{ form_end(edit_form) }}


{% endblock %}

and in my default controller file 并在我的默认控制器文件中

public function indexAction()
{

    $user = $this->container->get('security.context')->getToken()->getUser();
    $user->getId();
    $conn = $this->get('database_connection');

    $users = $conn->query("UPDATE user SET batman= 'thenum' WHERE username ='$user'");
    return $this->render('siteblogBundle:Default:index.html.twig', array('edit_form' => $users));
}

but I get this error when I run my code 但是我运行代码时收到此错误

Type error: Argument 1 passed to Symfony\\Component\\Form\\FormRenderer::renderBlock() must be an instance of Symfony\\Component\\Form\\FormView, instance of Doctrine\\DBAL\\Driver\\PDOStatement given, called in /opt/lampp/htdocs/x/chessMult/1/sym/thesym/app/cache/dev/twig/96/96f6b6f8c21d5412246839a2d6e2eb66ac4141143dd67e2b91f285f2a31b60fc.php on line 44 类型错误:传递给Symfony \\ Component \\ Form \\ FormRenderer :: renderBlock()的参数1必须是Symfony \\ Component \\ Form \\ FormView的实例,Doctrine \\ DBAL \\ Driver \\ PDOStatement的实例,在/ opt / lampp /中调用htdocs / x / chessMult / 1 / sym / thesym / app / cache / dev / twig / 96 / 96f6b6f8c21d5412246839a2d6e2eb66ac4141143dd67e2b91f285f2a31b60fc.php在第44行

You should use a Symfony Form . 您应该使用Symfony表单 The returned data from the form can be flushed to update your database record. 表单中返回的数据可以刷新以更新数据库记录。 There is an example provided in the docs. 文档中提供了一个示例。

$task = new Task();

$form = $this->createFormBuilder($task)
    ->add('task', TextType::class)
    ->add('dueDate', DateType::class)
    ->add('save', SubmitType::class, array('label' => 'Create Task'))
    ->getForm();

$form->handleRequest($request);

if ($form->isSubmitted() && $form->isValid()) {
    // $form->getData() holds the submitted values
    // but, the original `$task` variable has also been updated
    $task = $form->getData();

    // ... perform some action, such as saving the task to the database
    // for example, if Task is a Doctrine entity, save it!
    // $em = $this->getDoctrine()->getManager();
    // $em->persist($task);
    // $em->flush();

    return $this->redirectToRoute('task_success');
}

return $this->render('default/new.html.twig', array(
    'form' => $form->createView(),
));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM