如何更新学说中的字段以将其设置为空 [英] How to update a field in doctrine to set it null

查看:94
本文介绍了如何更新学说中的字段以将其设置为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将教义中的一个字段设置为空,这是句子

I want to set null to a field in doctrine and here is the sentence

$em = $this->getDoctrine()->getManager();
$qb = $em->createQueryBuilder();
$query = $qb->update('Model\Example', 'u')->set('u.deletedAt', ':deletedAt')
->where("u.id IN (:ids)")->setParameter('deletedAt', null)
->setParameter('ids', $ids)
->getQuery();
 $query->execute();

我认为这段代码应该可以完成这项工作,但我遇到了这个异常

i think that this code should do the job, but im getting this exception

执行 'UPDATE example SET deleted_at = 时发生异常WHERE (id IN (?)) AND (example.deleted_at IS NULL)' 带参数[null, "5,6"]: SQLSTATE[22P02]: 文本表示无效:7 错误:la sintaxis de entrada no es válida para integer: «5,6»

An exception occurred while executing 'UPDATE example SET deleted_at = ? WHERE (id IN (?)) AND (example.deleted_at IS NULL)' with params [null, "5,6"]: SQLSTATE[22P02]: Invalid text representation: 7 ERROR: la sintaxis de entrada no es válida para integer: «5,6»

首先为什么学说要添加 AND (example.deleted_at IS NULL) 我做错了什么?

first of all why doctrine is adding that AND (example.deleted_at IS NULL) am i doing something wrong ?

推荐答案

您的原始查询看起来应该有效.我复制并测试:

Your original query looks like it should work. I duplicated and tested with:

    $em = $this->getService('doctrine.orm.entity_manager');
    $qb = $em->createQueryBuilder();

    $qb->update('Cerad\Bundle\PersonBundle\Entity\Person','person');

    $qb->set('person.verified',':verified');
    $qb->setParameter('verified',null);

    $qb->where('person.id IN (:ids)');
    $qb->setParameter('ids',array(1,2,3));

    echo $qb->getQuery()->getSql(); // UPDATE persons SET verified = ? WHERE id IN (?)

    $qb->getQuery()->execute();

按预期工作.

您确定复制/粘贴了确切的代码吗?事后没有编辑?验证您的 ids 数组确实是一个整数数组.这是我能看到的唯一可能存在问题的地方.并确保您的错误来自您发布的代码.也许还有其他事情发生?尝试在命令对象中隔离您的代码.当然,deletedAt 是否可以为 null 设置为 true?

Are you sure you copy/pasted your exact code? No editing after the fact? Verify your ids array really is an array of integers. That is the only spot I could see where there might be an issue. And do make sure your error is coming from the code you posted. Maybe something else is going on? Try isolating your code in a command object. And of course deletedAt has it's is nullable set to true?

在这种情况下没有真正需要使用 expr 对象.Doctrine 2 正确处理 IN 语句的数组.

There is no real need to use the expr object for this case. Doctrine 2 correctly handles arrays for IN statements.

====================================

====================================

我怀疑你有 $ids = '5,6'?尝试将其设置为: $ids = array(5,6);尽管即使使用字符串,我也看不出它是如何弄乱查询的.

I suspect you have $ids = '5,6'? Try setting it to: $ids = array(5,6); Though even with a string I don't see how it's messing up the query.

这篇关于如何更新学说中的字段以将其设置为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆