I have two entities viz WallPost
and WallPostLike
and they are related by OneToMany relationship.
WallPost.php
#[ORMTable(name: "wall_posts")]
class WallPost
{
use UserIdTrait;
use DistrictIdTrait;
use ModifiedTrait;
#[ORMOneToMany(mappedBy: 'wallPost', targetEntity: WallPostLike::class, fetch: 'EAGER')]
private Collection $wallPostLikes;
public function __construct()
{
$this->wallPostLikes = new ArrayCollection();
}
/**
* @return Collection<int, WallPostLike>
*/
public function getWallPostLikes(): Collection
{
return $this->wallPostLikes;
}
}
WallPostLike.php
#[ApiResource(
shortName: 'WallPostLike',
paginationEnabled: false,
graphQlOperations: [
new Mutation(
resolver: WallPostLikesResolver::class,
args: [
WallPostLikeEnum::WALL_POST_ID => ['type' => 'String!'],
PostParam::CURRENT_SCHOOL_ID => ['type' => 'String!'],
],
name: 'toggle',
),
],
)]
#[ORMTable(name: "wall_post_likes")]
class WallPostLike
{
use ModifiedTrait;
#[ORMManyToOne(targetEntity: WallPost::class, inversedBy: 'wallPostLike')]
#[ORMJoinColumn(name: 'wall_post_id', referencedColumnName: 'id', nullable: false)]
private WallPost $wallPost;
public function getWallPost(): WallPost
{
return $this->wallPost;
}
public function setWallPost(WallPost $wallPost): self
{
$this->wallPost = $wallPost;
return $this;
}
}
The WallPost
Entity has modified
column to track the timestamp when entity updates. The problem is the timestamp of modified
column is updated when I call toggleWallPostLike
API to like a post. The strange thing is the timestamp is already updated even before reaching the resolver function of the requested API.
ModifiedTrait.php
<?php
declare(strict_types=1);
namespace AppTraitsEntity;
use DateTime;
use DoctrineORMMapping as ORM;
use GedmoMappingAnnotationTimestampable;
trait ModifiedTrait
{
#[Timestampable(on: 'update')]
#[ORMColumn(name: 'modified', type: 'datetime')]
private DateTime $modified;
public function getModified(): DateTime
{
return $this->modified;
}
public function setModified(DateTime $modified): self
{
$this->modified = $modified;
return $this;
}
}
To check, whether something is really updated, I have added PreUpdate
Lifecycle event in WallPost
entity and to my surprise two of the fields viz user_id
and district_id
of this entity has changes in their value as shown in following image
Here is how UserIdTrait.php looks like for reference
<?php
declare(strict_types=1);
namespace AppTraitsEntity;
use DoctrineORMMapping as ORM;
use DoctrineORMMappingColumn;
use SymfonyBridgeDoctrineIdGeneratorUuidGenerator;
trait UserIdTrait
{
#[Column(name: 'user_id', type: 'uuid')]
#[ORMGeneratedValue('CUSTOM')]
#[ORMCustomIdGenerator(UuidGenerator::class)]
private string $userId;
public function getUserId(): string
{
return $this->userId;
}
public function setUserId(string $userId): self
{
$this->userId = $userId;
return $this;
}
}
I am not really sure how doctrine / Timestampable extension works behind the scene but this should not be the desired behavior.