Allow doctrine to work correctly with counters #7239

Open
opened 2026-01-22 15:47:43 +01:00 by admin · 0 comments
Owner

Originally created by @awwar on GitHub (Nov 3, 2023).

Feature Request

Q A
New Feature yes
RFC no
BC Break no

Summary

First of all, I want to say thank you to your team. Doctrine allows complex things to be made very simple, understandable and supportable.

But there are certain things that still have to be processed with raw queries.
For example, increasing counters:

#[Entity]
class Article
{
    #[Id, Column(type: 'integer'), GeneratedValue]
    private int $id;

    #[AsCounter] // <------ An attribute that marks a field as a counter
    #[Column(type: 'integer')]
    private int $views;

    #[Column(type: 'string')]
    private string $authorName;

    public function addViews(int $views): void
    {
        $this->views += $views;
    }

    public function getViews(): int
    {
        return $this->views;
    }

    public function setAuthorName(string $newName): void
    {
        $this->authorName = $newName;
    }
}

$article = $this->em->find('Article', 1);

echo $article->getViews(); // 13

$article->addViews(4);
$article->setAuthorName("Jon");

$this->em->flush();

What happens after flush now, without AsCounter attribute:

UPDATE articles SET author_name = 'Jon', views = 17  WHERE id = 1
                                                ^^^

But it would be cool if, thanks to the AsCounter attribute, it worked like this:

UPDATE articles SET author_name = 'Jon', views = views + 4 WHERE id = 1
                                                 ^^^^^^^^^
Originally created by @awwar on GitHub (Nov 3, 2023). ### Feature Request <!-- Fill in the relevant information below to help triage your issue. --> | Q | A |------------ | ------ | New Feature | yes | RFC | no | BC Break | no #### Summary First of all, I want to say thank you to your team. Doctrine allows complex things to be made very simple, understandable and supportable. But there are certain things that still have to be processed with raw queries. For example, increasing counters: ```php #[Entity] class Article { #[Id, Column(type: 'integer'), GeneratedValue] private int $id; #[AsCounter] // <------ An attribute that marks a field as a counter #[Column(type: 'integer')] private int $views; #[Column(type: 'string')] private string $authorName; public function addViews(int $views): void { $this->views += $views; } public function getViews(): int { return $this->views; } public function setAuthorName(string $newName): void { $this->authorName = $newName; } } $article = $this->em->find('Article', 1); echo $article->getViews(); // 13 $article->addViews(4); $article->setAuthorName("Jon"); $this->em->flush(); ``` What happens after `flush` now, without `AsCounter` attribute: ```sql UPDATE articles SET author_name = 'Jon', views = 17 WHERE id = 1 ^^^ ``` But it would be cool if, thanks to the `AsCounter` attribute, it worked like this: ```sql UPDATE articles SET author_name = 'Jon', views = views + 4 WHERE id = 1 ^^^^^^^^^ ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7239