原则 2:使用查询生成器更新查询

2022-01-16 00:00:00 sql database php doctrine

嗨我有以下查询,但它似乎不起作用.

Hi I've got the following query but it doesn't seem to work.

$q = $this->em->createQueryBuilder()
    ->update('modelsUser', 'u')
    ->set('u.username', $username)
    ->set('u.email', $email)
    ->where('u.id = ?1')
    ->setParameter(1, $editId)
    ->getQuery();
$p = $q->execute();

这将返回以下错误消息:

This returns the following error message:

致命错误:未捕获的异常'教义ORM查询查询异常'带有消息'[语义错误]行0, col 38 靠近 'testusername WHERE':错误:未定义testusername".在...

Fatal error: Uncaught exception 'DoctrineORMQueryQueryException' with message '[Semantical Error] line 0, col 38 near 'testusername WHERE': Error: 'testusername' is not defined.' in ...

我会很高兴得到任何帮助

I would be glad of any help

推荐答案

我觉得你需要使用 ->set() 让你的所有 values 参数都更安全:

I think you need to use ->set() It's much safer to make all your values parameters:

$queryBuilder = $this->em->createQueryBuilder();
$query = $queryBuilder->update('modelsUser', 'u')
        ->set('u.username', ':userName')
        ->set('u.email', ':email')
        ->where('u.id = :editId')
        ->setParameter('userName', $userName)
        ->setParameter('email', $email)
        ->setParameter('editId', $editId)
        ->getQuery();
$result = $query->execute();

相关文章