原则2:更新表集字段=字段+ 1,其中id = 1

时间:2022-02-11 06:48:16

I want to accomplish this (mysql) query:

我想完成这个(mysql)查询:

UPDATE table SET field = field + 1 WHERE id = 1

What is the correct way of doing the above using a Doctrine 2 entity?

使用教条2实体进行上述操作的正确方法是什么?

*Edit

*编辑

I'm looking for a way to do $entity->incrementField(), which execute the above mysql query on flush()

我正在寻找执行$entity->incrementField()的方法,它在flush()上执行上面的mysql查询

2 个解决方案

#1


1  

I can't seem to find a way to do this using the entity itself. I have solved this now by utilizing a regular DQL which does increment, and update model/entity with result.

我似乎找不到用实体本身来做这个的方法。我现在已经通过使用一个增量的常规DQL解决了这个问题,并使用结果更新模型/实体。

#2


-1  

It won't lead to the exact same query, but maybe this is what you are looking for?

它不会导致完全相同的查询,但也许这就是您要寻找的?

class MyEntity {
    private $field;

    public function incrementField() {
        $this->field++;
    }
}

#1


1  

I can't seem to find a way to do this using the entity itself. I have solved this now by utilizing a regular DQL which does increment, and update model/entity with result.

我似乎找不到用实体本身来做这个的方法。我现在已经通过使用一个增量的常规DQL解决了这个问题,并使用结果更新模型/实体。

#2


-1  

It won't lead to the exact same query, but maybe this is what you are looking for?

它不会导致完全相同的查询,但也许这就是您要寻找的?

class MyEntity {
    private $field;

    public function incrementField() {
        $this->field++;
    }
}