无法在 Symfony 4 中使用 Doctrine2 DBAL\Connection 插入数据
Not able to INSERT data with Doctrine2 DBAL\Connection in Symfony 4
我正在尝试使用 Doctrine2 DBAL\Connection 在 MySQL table 中插入一些数据(for tables 我不想要待映射),代码如下:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\DBAL\Connection;
class UtilsController extends AbstractController
{
/**
* @Route("/utils/fixer", name="utils_fixer")
*/
public function dataFixer(Connection $conn)
{
$sql = "INSERT INTO test(id, username) VALUES (':id', ':name')";
$id = 456;
$name = 'blabla';
$res = $conn->executeUpdate($sql, ['id' => $id, 'name' => $name]);
return $this->render('utils/index.html.twig', ['res' => $res]);
}
}
这是我的 table 的样子:
这给了我:"SQLSTATE[HY000]: General error: 1366 Incorrect integer value: ':id' for column 'id' at row 1"。
Id字段没有自动递增,只是一个测试table.
SELECT 语句返回数据正常。
值绑定似乎不起作用??
我用了这个学说documentation。
如果 $id 的值是自动递增的,你不应该插入它的值——我认为它是。
占位符不应被引用。
至少这给出了 executeUpdate() 方法的用法示例。
public function dataFixer(Connection $conn)
{
$sql = "INSERT INTO test(id, username) VALUES (:id, :name)";
$id = 456;
$name = 'blabla';
$res = $conn->executeUpdate($sql, ['id' => $id, 'name' => $name]);
return $this->render('utils/index.html.twig', ['res' => $res]);
}
我正在尝试使用 Doctrine2 DBAL\Connection 在 MySQL table 中插入一些数据(for tables 我不想要待映射),代码如下:
<?php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;
use Doctrine\DBAL\Connection;
class UtilsController extends AbstractController
{
/**
* @Route("/utils/fixer", name="utils_fixer")
*/
public function dataFixer(Connection $conn)
{
$sql = "INSERT INTO test(id, username) VALUES (':id', ':name')";
$id = 456;
$name = 'blabla';
$res = $conn->executeUpdate($sql, ['id' => $id, 'name' => $name]);
return $this->render('utils/index.html.twig', ['res' => $res]);
}
}
这是我的 table 的样子:
这给了我:"SQLSTATE[HY000]: General error: 1366 Incorrect integer value: ':id' for column 'id' at row 1"。
Id字段没有自动递增,只是一个测试table.
SELECT 语句返回数据正常。
值绑定似乎不起作用??
我用了这个学说documentation。
如果 $id 的值是自动递增的,你不应该插入它的值——我认为它是。
占位符不应被引用。
至少这给出了 executeUpdate() 方法的用法示例。
public function dataFixer(Connection $conn)
{
$sql = "INSERT INTO test(id, username) VALUES (:id, :name)";
$id = 456;
$name = 'blabla';
$res = $conn->executeUpdate($sql, ['id' => $id, 'name' => $name]);
return $this->render('utils/index.html.twig', ['res' => $res]);
}