Allow doctrine to work correctly with counters #7241

Closed
opened 2026-01-22 15:47:45 +01:00 by admin · 2 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 ^^^^^^^^^ ```
admin closed this issue 2026-01-22 15:47:45 +01:00
Author
Owner

@derrabus commented on GitHub (Nov 5, 2023):

Thank you for your proposal. I'm afraid this feature is a bit too specific to include it in the ORM library.

@derrabus commented on GitHub (Nov 5, 2023): Thank you for your proposal. I'm afraid this feature is a bit too specific to include it in the ORM library.
Author
Owner

@awwar commented on GitHub (Nov 5, 2023):

@derrabus Thanks for the answer. Is there any extension point to add this functionality to my project?

I know that i can add a custom type:

class CounterType extends Type
{
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) { /* ToDO */ }

    public function convertToPHPValue($value, AbstractPlatform $platform) { /* ToDO */ }

    public function convertToDatabaseValue(int $value, AbstractPlatform $platform) 
    { 
         return sprintf('%s %d', $columnName, $value);
    }

    public function getName()
    {
        return 'counter'
    }
}

But I don’t know how to specify the $columnName in the convertToDatabaseValue method.

Or is this not the right way and should be done differently?

@awwar commented on GitHub (Nov 5, 2023): @derrabus Thanks for the answer. Is there any extension point to add this functionality to my project? I know that i can add a custom type: ```php class CounterType extends Type { public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform) { /* ToDO */ } public function convertToPHPValue($value, AbstractPlatform $platform) { /* ToDO */ } public function convertToDatabaseValue(int $value, AbstractPlatform $platform) { return sprintf('%s %d', $columnName, $value); } public function getName() { return 'counter' } } ``` But I don’t know how to specify the `$columnName` in the `convertToDatabaseValue` method. **Or is this not the right way and should be done differently?**
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#7241