我有一个包含两列的表:price
和constant
。
我想对表中的所有条目运行更新,以基于constant * coefficient
更新price
。
我怎么能做这样的查询?
您可以使用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例如。
这样,每次保存实体时,它都会自动更新相关字段。