我有两列的表:price
和constant
。
我想对表中的所有条目运行更新以price
基于constant * coefficient
.
我怎么能做这样的查询?
我有两列的表:price
和constant
。
我想对表中的所有条目运行更新以price
基于constant * coefficient
.
我怎么能做这样的查询?
您可以使用 DQL 查询来做到这一点:
$em->createQuery('
UPDATE entityClass e
SET e.price = e.constant * 33;
');
您也可以使用原始 SQL 执行此操作:
$em->getConnection()->executeUpdate('
UPDATE entity_table_name AS e
SET e.price = e.constant * 33;
');
您还可以在 preUpdate 和 prePersist 原则事件上添加一个侦听器,以便自动更新这些值。
有关示例,请参见http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/events.html#preupdate 。
这样每次保存实体时,它都会自动更新相关字段。