/** * By hooking to the mapper initialization event, we can extend it * with the softDelete capacity. * * @param \Analogue\ORM\System\Mapper $mapper * @return void */ protected function registerSoftDelete(Mapper $mapper) { $entityMap = $mapper->getEntityMap(); // Add Scopes $mapper->addGlobalScope(new SoftDeletingScope()); $host = $this; // Register 'deleting' events $mapper->registerEvent('deleting', function ($entity) use($entityMap, $host) { $deletedAtField = $entityMap->getQualifiedDeletedAtColumn(); if (!is_null($entity->getEntityAttribute($deletedAtField))) { return true; } else { $time = new Carbon(); $entity->{$deletedAtField} = $time; // Launch an update instead $host->manager->mapper(get_class($entity))->store($entity); return false; } }); // Register RestoreCommand $mapper->addCustomCommand('Analogue\\ORM\\Plugins\\SoftDeletes\\Restore'); }