spotorm / spot2

Spot v2.x DataMapper built on top of Doctrine's Database Abstraction Layer
http://phpdatamapper.com
BSD 3-Clause "New" or "Revised" License
601 stars 101 forks source link

On update current timestamp #248

Closed nenadmilosavljevic closed 6 years ago

nenadmilosavljevic commented 6 years ago

Hello, what would be the best way to save current timestamp when an entity is updated? Ideally, if triggers are supported I'd like to define DateTime property which would be updated with current DateTime on each ON_UPDATE Otherwise, how can I define on_update_current_timestamp for a timestamp field in an model class (which extends entity)?

tuupola commented 6 years ago

You could do something like:

    public static function events(EventEmitter $emitter)
    {
        $emitter->on("beforeUpdate", function (EntityInterface $entity, MapperInterface $mapper) {
            $entity->updated_at = new \DateTime();
        });
    }
FlipEverything commented 6 years ago

I think that @tuupola's answer is the preferred way, but if you want to use timestamps (like me) you can define a custom type.