php - 使用 Symfony 和 Doctrine 更新最后一个数据库条目

标签 php mysql symfony doctrine

首先,请原谅更多糟糕的代码,我对 PHP/Symfony/Doctrine 还比较陌生。

我试图用下面的代码实现的目标如下,我需要更新最后一个数据库条目的“Notification_ID”字段。现在,代码的上半部分正在检索最后一个数据库条目的 ID,然后我可以用代码的后半部分引用该条目,然后将通知 ID(通过 POST 输入)添加到该数据库条目。

 $em = $this->getDoctrine()->getManager();
    $query = $em->createQuery(
        'SELECT z.id
        FROM AppBundle:ZeusUsers z
        ORDER BY z.id DESC');
    $query->setMaxResults(1);
    $id = $query->getSingleScalarResult();

    $notificationid = $this->getRequest()->get("notification_id");

    $query = $em->createQuery(
        'UPDATE AppBundle:ZeusUsers z
        SET z.notification_id = $notificationid
        WHERE z.id = $id');
    $query->execute();

    $em->flush();

    return new Response("", 200, array("content-type"=>"text/html"));

上面更新了代码

现在返回以下错误:

[Syntax Error] line 0, col 57: Error: Expected Literal, got '$' (500 Internal Server Error)

从查询外部引用变量似乎有问题,有什么想法吗?

我如何在 native Symfony 中执行第二个查询,而不是像我一样使用查询函数?

最佳答案

设法使其正常运行:

$em = $this->getDoctrine()->getManager();
$query = $em->createQuery(
    'SELECT z.id
    FROM AppBundle:ZeusUsers z
    ORDER BY z.id DESC');
$query->setMaxResults(1);
$id = $query->getSingleScalarResult();

$notification_id = $this->getRequest()->get("notification_id");

$query1 = $em->createQueryBuilder();
$q = $query1->update('AppBundle:ZeusUsers', 'z')
    ->set('z.notification_id', '?1')
    ->where('z.id = ?2')
    ->setParameter(1, $notification_id)
    ->setParameter(2, $id)
    ->getQuery();
$p = $q->execute();

return new Response("", 200, array("content-type"=>"text/html"));

感谢那些帮助过我的人。

关于php - 使用 Symfony 和 Doctrine 更新最后一个数据库条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29375832/

相关文章:

angularjs - Symfony 和 Angular 应用程序

javascript - Jquery 使用 twig 值更改按钮的 url

php - 在循环内将数组添加到多维数组

php - 在 Laravel 中搜索两个数字(价格)

PHP 查询 : How to input data into 2 columns column on the same table at different times

php - 无法从容器中获取 IriConverter,因为它不是公共(public)的

php - 如何查找同一天同一列的两个日期之间的差异?

php - MYSQL/PHP 帮助 - 从原始预订两侧的表格中选择下一个可用日期

php - $_POST 接收要更新的动态字段数量

MySQL 如何更新另一个表中同一行的多个列