Allow doctrine to work correctly with counters #11043
-
Feature Request
SummaryFirst 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. #[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 UPDATE articles SET author_name = 'Jon', views = 17 WHERE id = 1
^^^ But it would be cool if, thanks to the UPDATE articles SET author_name = 'Jon', views = views + 4 WHERE id = 1
^^^^^^^^^ |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 2 replies
-
Thank you for your proposal. I'm afraid this feature is a bit too specific to include it in the ORM library. |
Beta Was this translation helpful? Give feedback.
-
@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 Or is this not the right way and should be done differently? |
Beta Was this translation helpful? Give feedback.
-
You can use a second property with the additional value, a post update+persist listener and unitofwork scheduleExtraUpdate API or directly issue a second update at that point. |
Beta Was this translation helpful? Give feedback.
You can use a second property with the additional value, a post update+persist listener and unitofwork scheduleExtraUpdate API or directly issue a second update at that point.