I want to detect if a new record was inserted or an old record was updated in afterSave(), but I have no idea how.
I thought about writing a config variable in the beforeSave() (where I can detect an insert, if the id isn't set) but maybe that's not a safe way of doing it.
Any ideas?
You can do it using afterSave( $model, $created ) function.
Because, the second argument of the afterSave()
function is Boolean
and will true
if a new row is created else it will gives you false
.
You can check this by writing the following lines of code here:
public function afterSave(Event $event, Entity $entity, array $options) {
if ($entity->isNew()) {
// do whatever you need to do here.
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With